0

How would you make an if/else statement loop back to the beginning to get user information?

This is the code I got so far:

var age = prompt("Please enter your age");

if(age == 21 ) {
    alert("Happy 21st Birthday!");
} else if (age > 21 ) {
    alert("You are old");
} else { 
    alert("Please enter an age");
}

I'm trying to make it go back to the beginning to make the user enter information.

2
  • Make it loop by surrounding the statements with a loop: while (not answered) {if (age == 21) .... ` }` Commented Sep 10, 2017 at 2:19
  • Possible duplicate of Loop that prompts user until valid input Commented Sep 10, 2017 at 2:30

4 Answers 4

4
var age = '';
while(age == '' || age == 'ok'){   
   age = prompt("Please enter your age");
   if($.isNumeric(age) === false){
       continue;
   }
   if(age == 21 ){
        alert("Happy 21st Birthday!");
        continue;
   }
   if (age > 21 ){
        alert("You are old");
        continue;
   }
   if (age < 21){ 
       alert("You are too young to be in this bar!");  
   }
}
Sign up to request clarification or add additional context in comments.

9 Comments

while(true) will keep looping forever, no?
Yes it will. Did you want it to stop at any time?
OP didn't specify, but it sounds like they wanted it to loop until the user answered the question.
It will now stop asking if you are 21 or older. This is how it should be at a bar.
But if someone < 21 tries to use it, they'll get an endless loop.
|
0
for (let age = prompt('Please enter your age'); ;) {
  if (age == 21) {
    alert('Happy 21st Birthday!');
    break;
  } else if (age > 21) {
    alert('You are old');
    break;
  } else {
    age = prompt('Please enter your age');
  }
}

Comments

0

You separate validation logic from the user input logic.

If this is console app then you would place a loop around the prompt and then validate the user age, if the age is valid break out of the loop otherwise let the loop continue.

On a web page you would wrap it in a function and based on the result manipulate the view based on if the age is correct or not. So you would perhaps show an error message if the age is invalid or go to the next page if the age is valid.

You should wrap the if statements which make up the validation logic in into a function perhaps validateAge that returns true or false, that way no matter what you implement you can use the same method.

Comments

0

Working off of @wallyk's suggestion using a while loop yields this example:

var age = false;

while (!age) {

  age = prompt("Please enter your age");

  if (age == 21) {
    alert("Happy 21st Birthday!");
  } else if (age > 21) {
    alert("You are old");
  } else if (!!age && age < 21) {
    alert("You are young");
  } else {
    alert("Please enter an age");
    age = false;
  }
}

It will keep looping until you type a valid number answer. I also added in a check to see if the user input an age under 21, since I'm guessing you don't want to keep looping forever if the user is under 21 (or keep looping until they turn 21) but that part can easily be removed if you want.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.