1

I am trying to make a simple thing that changes the value of the paragraph based on a prompt. I have the javascript encoded into the html. But when I run it, the screen is blank. The prompt doesn't even show up. I am a beginner, so this is probably easy to solve, but could someone please help? Thank you in advance.

<!DOCTYPE html>
<html>
    <head>
        <title>Randomness!</title>
    </head>

    <body>
        <p id="change"> </p>

        <script>
            var change = prompt("Enter 1 or 2");

            if (change === 1) {
                document.getElementById("change").innerHTML = "You said one!";
            }

            if change === 2 {
                document.getElementById("change").innerHTML = "You said two!";
            }

            else {
                document.getElementById("change").innerHTML = "That was not an option.";
            }
        </script>
    </body>
</html>

5 Answers 5

4

Syntax error . Correct below if condition to make it work

  if (change === '2') {

http://codepen.io/nagasai/pen/yJRQNO

To make the functionality to work, update if condition comparison to string values 1 and 2

HTML:

<html>
    <head>
        <title>Randomness!</title>
    </head>

    <body>
        <p id="change"> </p>

        <script>
            var change = prompt("Enter 1 or 2");
console.log(change)
            if (change === '1') {
                document.getElementById("change").innerHTML = "You said one!";
            }

            else if (change === '2') {
                document.getElementById("change").innerHTML = "You said two!";
            }

            else {
                document.getElementById("change").innerHTML = "That was not an option.";
            }
        </script>
    </body>
</html>
3
  • should be else if Commented Aug 5, 2016 at 23:30
  • yes @AdamBuchananSmith , thanks ...updated codepen :)
    – Naga Sai A
    Commented Aug 5, 2016 at 23:32
  • you should update answer on here as well really as yeah you fixed the syntax but if change === 1 then his output will be the end else clause all the time Commented Aug 5, 2016 at 23:35
1

You need surround your if condition this will work below.. you should make this aelse ifas well because if the value of change is 1 it will always go into the last else clause.. (a switch statement would of been better probably) anyway here this will work.

   var change = prompt("Enter 1 or 2");

   if (change === '1') {
      document.getElementById("change").innerHTML = "You said one!";
   }

   else if (change === '2') {
       document.getElementById("change").innerHTML = "You said two!";
   }
   else {
       document.getElementById("change").innerHTML = "That was not an option.";
  }
3
  • Thanks, I completely forgot about else if. Commented Aug 5, 2016 at 23:36
  • I was going to mention the need for the single quotes, you caught it before I commented :) Commented Aug 5, 2016 at 23:40
  • ha thanks.. yeah i just noticed it before i clicked off the question! @AdamBuchananSmith Commented Aug 5, 2016 at 23:41
0

Based on my opinion there is a syntax error on the second if statement. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/if...else

1
  • P.S. Could you please rate my question +1? I am new and only have 10 reputation. If you put it up one I should have fifteen and be able to rate your answer +1. Commented Aug 5, 2016 at 23:33
0

Three problems here.

1) change will be a string, not an integer
2) no parens around the second condition
3) condition should be an else if

Try:

var change = prompt("Enter 1 or 2");

if (change === '1') {
  document.getElementById("change").innerHTML = "You said one!";
} else if (change === '2') {
  document.getElementById("change").innerHTML = "You said two!";
} else {
  document.getElementById("change").innerHTML = "That was not an option.";
}
<p id="change"> </p>

0

change, when prompted, is a String. You are comparing it to a Number using ===, which checks for types, so it's not going to work.

You shoud either:

  • Use parseInt to convert the entered String to a Number:

    var change = parseInt( prompt("Enter 1 or 2"), 10);

  • Use ==, which will ignore types:

    if(change == 1)

  • Use Strings as comparisons:

    if(change === "1")

You're also missing parenthesis around your second condition, and you should use else if right there.

1
  • if the prompt is returning a string, then this if(change === "1") is completely acceptable. Commented Aug 5, 2016 at 23:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.