{"id":3764,"date":"2023-07-18T01:17:25","date_gmt":"2023-07-18T01:17:25","guid":{"rendered":"https:\/\/nmi.cool\/advweb\/?page_id=3764"},"modified":"2025-09-17T14:58:27","modified_gmt":"2025-09-17T14:58:27","slug":"exercise-conditionals","status":"publish","type":"page","link":"https:\/\/nmi.cool\/advweb\/unit-two-javascript\/week-five\/exercise-conditionals\/","title":{"rendered":"Exercise: Conditionals"},"content":{"rendered":"\n<p>Conditional statements that act based on whether or not a value is true, are an important part of any programming language. So (<br>IF you are interesting in learning more about conditionals<br>) {<br>let&#8217;s get started.<br>}<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Create a new html file named conditional.html.&nbsp;Create a js folder and create a conditional.js file. <\/li>\n\n\n\n<li>Connect the two with some starter code:<img decoding=\"async\" src=\"https:\/\/apweb.quest\/wp-content\/uploads\/2019\/02\/img_5c54a4a4896cb.png\" alt=\"\"><pre class=\"wp-block-code\"><code>&lt;!DOCTYPE html&gt;<br>&lt;html lang=\"en\"&gt;<br>&lt;head&gt;<br>    &lt;meta charset=\"UTF-8\"&gt;<br>    &lt;title&gt;Conditionals Practice&lt;\/title&gt;<br>&lt;\/head&gt;<br>&lt;body&gt;<br><br>    &lt;h1&gt;What is the sum of 14 + 10?&lt;\/h1&gt;<br><br>    &lt;input type=\"number\" id=\"userInput\"&gt;<br>    &lt;button onclick=\"compare()\"&gt;Try It&lt;\/button&gt;<br><br>    &lt;p&gt;&lt;span id=\"response\"&gt;Your feedback will appear here.&lt;\/span&gt;&lt;\/p&gt;<br><br>    &lt;script src=\"js\/conditional.js\"&gt;&lt;\/script&gt;<br>&lt;\/body&gt;<br>&lt;\/html&gt;<\/code><\/pre><\/li>\n\n\n\n<li>Now, let&#8217;s get the basic function set up in conditional.js. <pre class=\"wp-block-code\"><code>function compare() {\n    let answer = 24;\n    let response = document.querySelector(\"#response\");\n    response.innerHTML = \"correct\";\n}<\/code><\/pre><\/li>\n\n\n\n<li>Test in your browser. You should see \u201ccorrect\u201d appear in the span when you click the button. Change answer to something other than 24 and test again. Nothing changes yet! That&#8217;s next. <\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\">Create a conditional function.<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Replace your current function with a conditional function.<pre class=\"wp-block-code\"><code>function compare() {\n    let answer = document.querySelector(\"#userInput\");\n    answer = answer.value;\n    answer = Number(answer); \/\/ Convert from string to number\n\n    let response = document.querySelector(\"#response\");\n\n    if (answer === 24) {\n        response.innerHTML = \"Correct!\";\n    } else {\n        response.innerHTML = \"Wrong answer!\";\n    }\n}<\/code><\/pre><\/li>\n\n\n\n<li>Now try it: Enter 24 \u2192 \u201cCorrect!\u201d, Enter any other number \u2192 \u201cWrong answer!\u201d<\/li>\n\n\n\n<li>Let\u2019s offer smarter feedback based on how close the user is. Replace your existing conditional block with:<br><pre class=\"wp-block-code\"><code>if (answer === 24) {<br>    response.innerHTML = \"Correct!\";<br>} else if (answer &lt; 10) {<br>    response.innerHTML = answer + \" is way too low.\";<br>} else if (answer &lt; 24) {<br>    response.innerHTML = answer + \" is too low but close.\";<br>} else if (answer &gt; 40) {<br>    response.innerHTML = answer + \" is way too high.\";<br>} else {<br>    response.innerHTML = answer + \" is too high but close.\";<br>}<\/code><\/pre><\/li>\n\n\n\n<li>Try it out with different numbers and watch the feedback change!<\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\">Let&#8217;s switch it up.<\/h4>\n\n\n\n<p>You can replace all those if statements with a cleaner structure using a switch statement.<\/p>\n\n\n\n<p>Try this out in your function:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function compare() {\n    let answer = Number(document.querySelector(\"#userInput\").value);\n    let response = document.querySelector(\"#response\");\n\n    switch (true) {\n        case (answer === 24):\n            response.innerHTML = \"Correct!\";\n            break;\n        case (answer &lt; 10):\n            response.innerHTML = answer + \" is way too low.\";\n            break;\n        case (answer &lt; 24):\n            response.innerHTML = answer + \" is too low but close.\";\n            break;\n        case (answer > 40):\n            response.innerHTML = answer + \" is way too high.\";\n            break;\n        default:\n            response.innerHTML = answer + \" is too high but close.\";\n    }}\n<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Convert the input field to a dropdown. <\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Comment out the current input field in your html. <pre class=\"wp-block-code\"><code>&lt;!-- &lt;input type=\"number\" id=\"userInput\"&gt; --&gt;<\/code><\/pre><\/li>\n\n\n\n<li>Replace it with: <pre class=\"wp-block-code\"><code>&lt;select id=\"userInput\"&gt;&lt;\/select&gt;<\/code><\/pre><\/li>\n\n\n\n<li>In your JavaScript, generate dropdown options. At the top of your conditional.js file, add this loop: <pre class=\"wp-block-code\"><code>let dropdown = document.querySelector(\"#userInput\");<br><br>for (let i = 0; i &lt;= 100; i++) {<br>    dropdown.innerHTML += `&lt;option value=\"${i}\"&gt;${i}&lt;\/option&gt;`;<br>}<\/code><\/pre>This dynamically creates 100 options (0\u2013100) in your dropdown.<\/li>\n\n\n\n<li>Let&#8217;s automatically run the compare function when a selection is made. Update your html to make the button unnecessary:<pre class=\"wp-block-code\"><code>&lt;select id=\"userInput\" onchange=\"compare()\"&gt;&lt;\/select&gt;<\/code><\/pre><\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>Conditional statements that act based on whether or not a value is true, are an important part of any programming language. So (IF you are interesting in learning more about conditionals) {let&#8217;s get started.} Create a conditional function. Let&#8217;s switch it up. You can replace all those if statements with a cleaner structure using a &hellip; <a href=\"https:\/\/nmi.cool\/advweb\/unit-two-javascript\/week-five\/exercise-conditionals\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Exercise: Conditionals<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":3692,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-3764","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/nmi.cool\/advweb\/wp-json\/wp\/v2\/pages\/3764","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nmi.cool\/advweb\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/nmi.cool\/advweb\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/nmi.cool\/advweb\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nmi.cool\/advweb\/wp-json\/wp\/v2\/comments?post=3764"}],"version-history":[{"count":15,"href":"https:\/\/nmi.cool\/advweb\/wp-json\/wp\/v2\/pages\/3764\/revisions"}],"predecessor-version":[{"id":4485,"href":"https:\/\/nmi.cool\/advweb\/wp-json\/wp\/v2\/pages\/3764\/revisions\/4485"}],"up":[{"embeddable":true,"href":"https:\/\/nmi.cool\/advweb\/wp-json\/wp\/v2\/pages\/3692"}],"wp:attachment":[{"href":"https:\/\/nmi.cool\/advweb\/wp-json\/wp\/v2\/media?parent=3764"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}