Note that there are some explanatory texts on larger screens.

plurals
  1. POIssues with using conditionals based on user input in JavaScript
    text
    copied!<p>I am building a joke website for some friends, and one page on the website I would like to have somewhat of an interactive game. I am used to C++ so the transition to javascript is an interesting one. All I have on the page for now is a paragraph element, a text input box and a button. When the button is clicked it is supposed to call the playGame() function. The playGame function looks at whats in the paragraph element ("game") and then look at the users input and generate the appropriate result. I am just using a set of conditionals to accomplish this but for some reason the function does not seem to change the paragraph element, thus making the entire thing a dud, of gone through and made some revisions but I have not been able to fix the issue. Here is the code:</p> <pre><code>&lt;html&gt; &lt;script&gt; function playGame() { test=document.getElementById("game"); var userInput=gameInput=document.getElementById("gameInput").value; if (test.innerHTML=="Question_1") { if (userInput.toLowerCase()=="yes") { test.innerHTML="Ending_Result_1"; } else if (userInput.toLowerCase()=="no") { test.innerHTML="Question_2"; } else { test.innerHTML="Ending_Result_2"; } } else if (test.innerHTML=="Question_2") { if (userInput.toLowerCase()=="yes") { test.innerHTML="Positive_Ending"; } else if (userInput.toLowerCase()=="no") { test.innerHTML="Ending_Result_3"; } else { test.innerHTML="Ending_Result_4"; } else { test.innerHTML="Refresh_page_message"; } } &lt;/script&gt; &lt;head&gt; &lt;title&gt;CptTuna's Whip or No Whip&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;p id="game"&gt;"Question_1"&lt;/p&gt; &lt;input id="gameInput" type="text"&gt; &lt;button type="button" onclick="playGame()"&gt;Answer&lt;/button&gt; &lt;/html&gt; </code></pre> <p>I just changed the actual text content to generic phrases for the sake of appropriateness. Any help on why the function is not executing properly would be greatly appreciated. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload