1

How can I continue prompting a user for a valid response using if... else if statements? My script currently works once, but then breaks:

var enterNum = prompt("Please enter a number between 1 and 100", "");


if (isNaN(enterNum)){
    enterNum = prompt("You did not enter a valid number. Please try again", "")
}
else if (enterNum < 1 || enterNum >100){
    enterNum = prompt("Your number is not between 1 and 100. Please try again", "")
}
else{
    document.write("Your number is ", enterNum)
}

Thanks in advance!

Annie
  • 51
  • 2
  • 4
  • I'm fairly certain this question has been answered by many people. Please accept an answer in the interest of sticking to the theology of the site. – kevlar1818 Aug 09 '11 at 12:21

5 Answers5

9
var enterNum = prompt("Please enter a number between 1 and 100", "");

while(isNaN(enterNum) || enterNum < 1 || enterNum >100) {
    enterNum = prompt("You did not enter a valid number. Please try again", "")
}
document.write("Your number is ", enterNum)
kevlar1818
  • 3,055
  • 6
  • 29
  • 43
  • You shouldn't need to specifically tell the client that they were out of bounds with something this simple. – kevlar1818 Aug 05 '11 at 18:46
  • While this solution is clean and efficient, my point was that I would like to be able to tailor my response to the user instead of a global error message. – Annie Aug 05 '11 at 21:24
  • Fair enough. Just clarifying there is a very simple way to do it. – kevlar1818 Aug 05 '11 at 21:38
1

You can't with only if/else. Use a loop. Example:

var enterNum = prompt("Please enter a number between 1 and 100", "");
while(true)
{
  if (isNaN(enterNum)){
      enterNum = prompt("You did not enter a valid number. Please try again", "")
  }
  else if (enterNum < 1 || enterNum >100){
      enterNum = prompt("Your number is not between 1 and 100. Please try again", "")
  }
  else
    break;
}
document.write("Your number is ", enterNum)
yankee
  • 38,872
  • 15
  • 103
  • 162
1
var valid = false;
var msg = "Please enter a number between 1 and 100";

while(!valid){
    var enterNum = prompt(msg, "");

    if (isNaN(enterNum)){
        msg = "You did not enter a valid number. Please try again";
    }
    else if (enterNum < 1 || enterNum >100){
        msg = "Your number is not between 1 and 100. Please try again";
    }
    else{
        valid = true;
        document.write("Your number is ", enterNum)
    }
}

There are a bunch of other ways to do a similar thing, somewhat depending on style. This went for readability. Could also eliminate the valid variable and simply have while(true) then break once the input is correct. The document.write could also be after the while.

James Montagne
  • 77,516
  • 14
  • 110
  • 130
0

use a while loop instead

http://help.dottoro.com/ljqepqhd.php#dowhile

or

http://help.dottoro.com/ljqepqhd.php#while

Antony Scott
  • 21,690
  • 12
  • 62
  • 94
  • i thought i might get that because i linked to them! can you suggest an alternative website? – Antony Scott Aug 05 '11 at 18:47
  • w3schools is crappy. Use the alternatives listed in w3fools.com. My favs are SitePoint and MDN (also [dottoro](http://help.dottoro.com/) sometimes) – Mrchief Aug 05 '11 at 18:54
  • thanks, i will in the future. w3schools stlls tops google searches though. I've read w3fools, so know not to use them, but i figured how wrong can they get a while loop! But you're (both) right to mention the problem with the site forthe benefit of others. I suppose I could claim I meant to post a link to them to provoke this exact reaction, but I didn't. Shame on me :( – Antony Scott Aug 05 '11 at 19:45
  • And now you've helped increase their rank on google by linking to them! :) – James Montagne Aug 05 '11 at 21:32
  • dammit! I've changed the link(s) – Antony Scott Aug 05 '11 at 21:40
-1

I hate javascript so my syntax is probably off but something like:

var isValid = false;
var message = "Please enter a number between 1 and 100";

while(isValid == false)
{
    var enterNum = prompt(message, "");

    if (isNaN(enterNum)){
        isValid = false;
        message = "You did not enter a valid number. Please try again";
    }
    else if (enterNum < 1 || enterNum >100){
        isValid = false;
        message = "Your number is not between 1 and 100. Please try again";
    }
    else{
        isValid = true;
        document.write("Your number is ", enterNum)
    }
}

Using a do/while loop might be a little neater.

xr280xr
  • 12,621
  • 7
  • 81
  • 125
  • 1
    Yes it is off and a better one already posted. Comparison is ==, or ===, not = and is not needed with booleans – mplungjan Aug 05 '11 at 19:02
  • Btw. control structures have pretty much the same syntax in all "C based" languages ;) – Felix Kling Aug 05 '11 at 19:52
  • @mplungjan- Good catch. Comparison is not needed, but is clearer for someone who's never heard of a loop. And there were no answers when I started writing. – xr280xr Aug 08 '11 at 15:18