Vercity Official
Vercity Official

Reputation: 30

Validate response from prompt(). Prompt again on invalid response

I want to make it so that if the user doesn't enter "1" or "2", the question must be re-answered. I tried prompt{choice1}; but it doesn't work.

Any solutions?

var choice1 = prompt("You see a bear on your campsite, What do you do ? Type 1 if you start running into the woods or type 2 if you fight the bear.");

if (choice1 == "1") {

  for (var i = 2; i < 3; i++) {
    alert("You start running into the woods. You stop, Out of breathe and realize you somehow got cut in your left arm.");
  }

} else if (choice1 == "2") {

  for (var b = 0; b < 1; b++) {
    alert("You look around you to find something that could help you fight off the bear. You see a rock and you pick it up. The bear is getting ready to attack and right away you throw the rock");
  }

} else {}

Upvotes: 1

Views: 321

Answers (4)

splay
splay

Reputation: 327

First off your loops around your alerts are completely unnecessary as they each run only once. But the fact that you set them up so that they only run once implies you have some knowledge about loops, which is curious...

Either way to answer your question you could place this block of code in a function, say called "prompt" then in the else block call the "prompt" function again this is a programming technique called recursion. But to be honest the best solution would be to use a while loop

while (choice1 !== "1" && choice1 !== "2") {
    //prompt user for input 
}

//Or even better imo
while(true) {
    //prompt user for input 
    if(choice1 === "1") {
       // do your thing
       break; // break out of the while loop 
    }
}

The first solution I presented could be improved by creating a hardcoded list out accepted options and seeing if choice1 is contained in the list

while(!acceptedAnswers.contains(choice1)) 

But even so, the second solution using the while(true) I believe to be easier to read and more extensible. Good luck!

Oh and you can have both for loops have the same variable name, "i" in this case, since they are in different scopes :)

Upvotes: 0

David
David

Reputation: 953

var choice1 = prompt("You see a bear on your campsite, What do you do? "
    + "Type 1 if you start running into the woods or type 2 if you fight the bear.");

while( choice != "1" && choice != "2" ) {
    choice1 = prompt("-- You must choose either 1 or 2.");
}
if (choice1 == "1") {
    alert("You start running into the woods. You stop, Out of breathe and realize you somehow got cut in your left arm.");
} else { // choice1 must be "2"
    alert("You look around you to find something that could help you fight off the bear. You see a rock and you pick it up. The bear is getting ready to attack and right away you throw the rock");
}

Upvotes: 0

Zakaria Acharki
Zakaria Acharki

Reputation: 67525

Working fiddle

Use do..while loop :

do {
  var choice1 = prompt("You see a bear on your campsite, What do you do ? Type 1 if you start running into the woods or type 2 if you fight the bear.");

  if (choice1 == "1") {

    for (var i = 2; i < 3; i++) {
      alert("You start running into the woods. You stop, Out of breathe and realize you somehow got cut in your left arm.");
    }

  } else if (choice1 == "2") {

    for (var b = 0; b < 1; b++) {
      alert("You look around you to find something that could help you fight off the bear. You see a rock and you pick it up. The bear is getting ready to attack and right away you throw the rock");
    }

  }
}
while (choice1 != "1" && choice1 != "2");

Hope this helps.

Upvotes: 2

yuriy636
yuriy636

Reputation: 11661

You could put your code in a function, and if the condition isn't met, run the function again:

function ask() {
  var choice1 = prompt("You see a bear on your campsite, What do you do ? Type 1 if you start running into the woods or type 2 if you fight the bear.");

  if (choice1 == "1") {
    for (var i = 2; i < 3; i++) {

      alert("You start running into the woods. You stop, Out of breathe and realize you somehow got cut in your left arm.");

    }
  } else if (choice1 == "2") {
    for (var b = 0; b < 1; b++) {
      alert("You look around you to find something that could help you fight off the bear. You see a rock and you pick it up. The bear is getting ready to attack and right away you throw the rock");
    }
  } else {
    ask();
  }
}
ask();

Upvotes: 1

Related Questions