<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Leon Si</title>
    <description>The latest articles on DEV Community by Leon Si (@leondreamed).</description>
    <link>https://dev.to/leondreamed</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F946908%2Fb6b84994-b746-45a6-979c-2ce5503d6e6d.jpeg</url>
      <title>DEV Community: Leon Si</title>
      <link>https://dev.to/leondreamed</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/leondreamed"/>
    <language>en</language>
    <item>
      <title>Less Than 1% of Programmers Can Spot This Bug…</title>
      <dc:creator>Leon Si</dc:creator>
      <pubDate>Mon, 17 Oct 2022 14:34:43 +0000</pubDate>
      <link>https://dev.to/leondreamed/less-than-1-of-programmers-can-spot-this-bug-1a6d</link>
      <guid>https://dev.to/leondreamed/less-than-1-of-programmers-can-spot-this-bug-1a6d</guid>
      <description>&lt;p&gt;Welcome to the first post in this new series, &lt;em&gt;Can you spot the bug?&lt;/em&gt; Here, you’ll be challenged to find the bug in a code snippet &lt;em&gt;without&lt;/em&gt; running it.&lt;/p&gt;

&lt;p&gt;Ok, I know what you’re thinking, &lt;em&gt;“this is such a contrived challenge; why would you ever need to debug code without running it?”&lt;/em&gt; 🤔&lt;/p&gt;

&lt;p&gt;Well, other than being a fun challenge to see if you’ve got an exceptional eye for the &lt;em&gt;Where’s Waldo?&lt;/em&gt; of bugs, being able to spot bugs without repeatedly running the code can save you a lot of time.&lt;/p&gt;

&lt;p&gt;…&lt;em&gt;especially&lt;/em&gt; if the bug doesn’t produce obvious error messages. (If you’ve ever had to spend hours debugging a logic bug, you know what I’m talking about!)&lt;/p&gt;

&lt;h1&gt;
  
  
  Rules
&lt;/h1&gt;

&lt;ol&gt;
&lt;li&gt;You may not run the code nor look at any error messages produced by the code. 🙈&lt;/li&gt;
&lt;li&gt;Each code snippet is accompanied by a specific scenario of the code’s unexpected behavior. Your task is to find the bug that causes this behaviour. 🔎&lt;/li&gt;
&lt;li&gt;If you want to challenge yourself, try not to use Google or refer to documentation! 😱&lt;/li&gt;
&lt;li&gt;The code will often contain tricky constructs that might mislead you into thinking a certain part is the buggy culprit when it’s actually valid behavior! For the greatest challenge, try limiting yourself to one guess! 🙂&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  A Quick Disclaimer
&lt;/h2&gt;

&lt;p&gt;If you aren’t able to spot a bug, it doesn’t mean you’re a bad programmer!&lt;/p&gt;

&lt;p&gt;Having an eye for spotting bugs comes with experience: as you work with more and more code, you’ll encounter and become familiar with more and more bugs, including the more obscure ones.&lt;/p&gt;

&lt;p&gt;This blog post series is primarily a teaching resource: a resource for you to learn about some of the more nuanced and non-obvious behaviors of common programming languages.&lt;/p&gt;

&lt;p&gt;With that said, put on your thinking cap and try to spot the following hidden bug! 🧢&lt;/p&gt;

&lt;h1&gt;
  
  
  Translating Text
&lt;/h1&gt;

&lt;p&gt;This is a bug that I encountered a few years ago near the beginning of my journey learning JavaScript. It’s a self-contained HTML file containing some HTML and some JavaScript that tries to translate some text:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;!DOCTYPE html&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;'text'&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Hello, world!&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;select&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"languages"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;option&lt;/span&gt; &lt;span class="na"&gt;value=&lt;/span&gt;&lt;span class="s"&gt;"english"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;English&lt;span class="nt"&gt;&amp;lt;/option&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;option&lt;/span&gt; &lt;span class="na"&gt;value=&lt;/span&gt;&lt;span class="s"&gt;"french"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;French&lt;span class="nt"&gt;&amp;lt;/option&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;option&lt;/span&gt; &lt;span class="na"&gt;value=&lt;/span&gt;&lt;span class="s"&gt;"spanish"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Spanish&lt;span class="nt"&gt;&amp;lt;/option&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;option&lt;/span&gt; &lt;span class="na"&gt;value=&lt;/span&gt;&lt;span class="s"&gt;"python"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Python&lt;span class="nt"&gt;&amp;lt;/option&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/select&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;button&lt;/span&gt; &lt;span class="na"&gt;onclick=&lt;/span&gt;&lt;span class="s"&gt;'translate()'&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Translate Text&lt;span class="nt"&gt;&amp;lt;/button&amp;gt;&lt;/span&gt;

    &lt;span class="nt"&gt;&amp;lt;script&amp;gt;&lt;/span&gt;
        &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;translate&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;selectedOption&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;querySelector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;select[name="languages"]&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;
            &lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;innerText&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="na"&gt;english&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Hello, world!&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="na"&gt;french&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Bonjour, monde!&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="na"&gt;spanish&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;¡Hola, mundo!&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="na"&gt;python&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;print("Hello, world!")&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
            &lt;span class="p"&gt;}[&lt;/span&gt;&lt;span class="nx"&gt;selectedOption&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This code renders some text in a &lt;code&gt;div&lt;/code&gt; element. Underneath it, there’s a dropdown that allows you to select a language out of four languages: English, French, Spanish, and Python.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Yes, Python is a language; I didn’t specify they had to be &lt;em&gt;spoken&lt;/em&gt; languages 😛&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Underneath the dropdown, there’s a &lt;em&gt;Translate Text&lt;/em&gt; button that when clicked, will call the &lt;code&gt;translate()&lt;/code&gt; function defined on line 14. The &lt;code&gt;translate()&lt;/code&gt; function replaces the text in the &lt;code&gt;div&lt;/code&gt; with the translation of “Hello, world!” in the selected language from the dropdown.&lt;/p&gt;

&lt;p&gt;However, if you try running this code in a browser and select a language from the dropdown (other than English), clicking the &lt;em&gt;Translate Text&lt;/em&gt; button doesn’t seem to do anything, no matter how many times you click it.&lt;/p&gt;

&lt;p&gt;There’s a bug in this code. Where do you think it could be? 🧐&lt;/p&gt;




&lt;p&gt;&lt;em&gt;For some hints and the full solution, check out the full blog post over at &lt;a href="https://dialect.so/blog/can-you-spot-the-bug-1"&gt;https://dialect.so/blog/can-you-spot-the-bug-1&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>programming</category>
      <category>html</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
