简体   繁体   中英

Can anyone tell me why my last 2 if else statements are not working? Brand new to JavaScript here

I cannot really figure out why my last 2 else/if statements are not working, are my first if and else/if statements interfering with them? (sorry if the code is difficult to read, i kept getting errors while trying to post)

var userGuess = Number(window.prompt("Pick a number between 1 and 10:", ""));
var secretNumber = Math.floor(Math.random() * 10) + 1;

do {
  if (userGuess < secretNumber) {
    window.alert("A little low");
    document.write("<p>The secret number is... " + secretNumber + " and you guessed " + userGuess + "</p>");
    userGuess = Number(window.prompt("Pick another number between 1 and 10:", ""));
  } else if (userGuess > secretNumber) {
    window.alert("A little high");
    document.write("<p>The secret number is... " + secretNumber + " and you guessed " + userGuess + "</p>");
    userGuess = Number(window.prompt("Pick another number between 1 and 10:", ""));
  } else if (userGuess <= 0) {
    window.alert("Is not between 1 and 10");
    document.write("You guessed... " + userGuess + " this is not between 1 and 10");
    userGuess = Number(window.prompt("Pick another number between 1 and 10:", ""));
  } else if (userGuess > 10) {
    window.alert("Is not between 1 and 10");
    document.write("You guessed... " + userGuess + " this is greater than 10");
    userGuess = Number(window.prompt("Pick another number between 1 and 10:", ""));
  }
} while (userGuess != secretNumber);

document.write("Congratulations, you are correct! The number is " + userGuess + "!");

A little logic error here: as per the comments above, you need to check if the input is valid before comparing it to your random number .

In Javascript, if/else statements are always checked in order , ie with your existing code, if a user picks 11, it will fall in your else if (userGuess > secretNumber) { block, as it is positioned before your else if (userGuess > 10) { block.

Just switch the order of the if/else statements so that your input validation code is before your code for comparison, and it should be fine:

var userGuess = Number(window.prompt("Pick a number between 1 and 10:", ""));
var secretNumber = Math.floor(Math.random() * 10) + 1;

do {
  //input validation:

  if (userGuess <= 0) {
    window.alert("Is not between 1 and 10");
    document.write("You guessed... " + userGuess + " this is not between 1 and 10");
    userGuess = Number(window.prompt("Pick another number between 1 and 10:", ""));
  } else if (userGuess > 10) {
    window.alert("Is not between 1 and 10");
    document.write("You guessed... " + userGuess + " this is greater than 10");
    userGuess = Number(window.prompt("Pick another number between 1 and 10:", ""));
  }

  //actual comparison:

  else if (userGuess < secretNumber) {
    window.alert("A little low");
    document.write("<p>The secret number is... " + secretNumber + " and you guessed " + userGuess + "</p>");
    userGuess = Number(window.prompt("Pick another number between 1 and 10:", ""));
  } 
else if (userGuess > secretNumber) {
    window.alert("A little high");
    document.write("<p>The secret number is... " + secretNumber + " and you guessed " + userGuess + "</p>");
    userGuess = Number(window.prompt("Pick another number between 1 and 10:", ""));
  }
} while (userGuess != secretNumber);

document.write("Congratulations, you are correct! The number is " + userGuess + "!");

you must replace your first two conditions with the other two conditions since the javascript would never get to there

  var userGuess = Number(window.prompt("Pick a number between 1 and 10:", 
    ""));
    var secretNumber = Math.floor(Math.random() * 10) + 1;

    do {
        if (userGuess <= 0) {
            window.alert("Is not between 1 and 10");
            document.write("You guessed... " + userGuess + " this is not between 1 and 10");
            userGuess = Number(window.prompt("Pick another number between 1 and 10:", ""));
        }
        else if (userGuess > 10) {
            window.alert("Is not between 1 and 10");
            document.write("You guessed... " + userGuess + " this is greater than 10");
            userGuess = Number(window.prompt("Pick another number between 1 and 10:", ""));
        }
        else if (userGuess > secretNumber) {
            window.alert("A little high");
            document.write("<p>The secret number is... " + secretNumber + " and you guessed " + userGuess + "</p>");
            userGuess = Number(window.prompt("Pick another number between 1 and 10:", ""));
        }
        else if(userGuess < secretNumber) {
            window.alert("A little low");
            document.write("<p>The secret number is... " + secretNumber + " and you guessed " + userGuess + "</p>");
            userGuess = Number(window.prompt("Pick another number between 1 and 10:", ""))
        }
    } while (userGuess != secretNumber);

    document.write("Congratulations, you are correct! The number is " + userGuess + "!");

You need to move the check for invalid input to before the check for a match. For example suppose the user enters "0". The way you have it written, this happens:

if (userGuess < secretNumber) {
    // this gets executed because the condition is true
} else if (userGuess > secretNumber) {
    // this does not
} else if (userGuess <= 0) {
    // nor does this even though the condition us true
} else if (userGuess > 10) {
    // or this
}

The if/else blocks are mutually exclusive. Once one gets entered, the others won't. And this happens in the order they appear in your code. Re-arrange to:

if (userGuess <= 0) {
    // When user enters "0" this gets executed
} else if (userGuess > 10) {
    // this does not
} else if (userGuess < secretNumber) {
    // this does not
} else if (userGuess > secretNumber) {
    // this does not
}

Another option (to simplify your code a bit) is to use a loop to read the user input and not exit the loop until the user has entered a valid value.

do {
    do {
        userGuess = Number(window.prompt("Pick a number between 1 and 10:", ""));
    } while (userGuess <= 0 || userGuess > 10);
    // It is now guaranteed that 0 < userGuess <= 10
    if (userGuess < secretNumber) {
        // Print msg
    }
    else if (userGuess > secretNumber) {
        // Print msg
    }
} while (userGuess != secretNumber);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM