1

I am new here and currently learning JavaScript. In the code below, how can I get it to loop and still prompt if say for example the user does not enter 1 or -1? Instead of using the else statement to output "This is not a valid input," I want it to prompt the user once again until they enter the desired input. I am not quite getting the desired out with the while loop.

var myShopping = ["Eggs", "Milk", "Potatoes", "Cereal", "Banana"];
var ord = prompt("Enter 1 for alphabetical order, " +
"and -1 for reverse order", 1);

if (ord == 1) {
    myShopping.sort();
    document.write(myShopping.join("<br />"));
} else if (ord == -1) {
    myShopping.sort();
    myShopping.reverse();
    document.write(myShopping.join("<br />"));
} else {
    document.write("That is not a valid input");
}
3
  • 1
    wrap it in a while loop? while(true){ your code here } Commented Jun 10, 2015 at 21:49
  • Just so you know, there's a special place in hell for people who use document.write. :P Also, prompt makes for a horrible UI, and is arguably entirely unnecessary in an HTML page. Commented Jun 10, 2015 at 21:56
  • I'm currently following a book and those are the instrcutions given, but i do sometimes omit it and use console.log instead. @cHao Commented Jun 11, 2015 at 10:29

3 Answers 3

1
var valid_ans = false;
while(!valid_ans) { // repeat this block until valid_ans is true
       // your code
       If (prompt===1 || prompt===-1) {
            valid_ans = true;
        }
  }

The above code should give you an idea. Keep in mind that your browser does not like multiple prompts. After two consecutive prompts the browser will ask the user if prompts should be suppressed.

Sign up to request clarification or add additional context in comments.

Comments

1

this is basically pseudo code, but should get you on the right track.. dont want to give you everything! best way to learn is through trial and error :)

var myShopping = ["Eggs", "Milk", "Potatoes", "Cereal", "Banana"];
while(true){
   //prompt here
   if(ord == 1){
       //do stuff
       break;
   }
   else if(ord == -1){
       //do stuff
       break;
   }
   else {
       //bad input
   }
}

we dont want to create the myShopping variable every time you go through the loop, so we put that outside of the loop... the prompt has to be inside incase they put in a bad character so that it will prompt them again.

some notes: document.write is pretty old and looks really bad so you should probably try to come up with a better way to do that... you may also want to put the logic of your if statements in a function you call so your code is neat

if you want to put it in a function then I would probably do something like this.

var doStuff = function(userInput){
    if(userInput == 1){
       //do stuff
       return true;
   }
   else if(userInput == -1){
       //do stuff
       return true;
   }
   else {
       //bad input
       return false;
   }
};

var myShopping = ["Eggs", "Milk", "Potatoes", "Cereal", "Banana"];
while(true){
    //prompt here
    var ord = prompt(... etc.);
    var toEndLoop = doStuff(ord);
    if(toEndLoop){
        break;
    } else {
        // they dun goofed.. tell them it was bad input and the prompt will happen again
    } 
}

3 Comments

@RickHitchcock I was in the process of changing it :)
Going by your code, I have managed to make the loop work without it being infinite. I also went a step further by doing what you suggest (putting the logic of the if statement in a function) but when I do so: say for example: declaring the function before the while loop and then calling the function inside the while loop - it becomes infinite once again. Any suggestions on how to use the function in the while loop?
@Chief ok I edited to include a function... something like that should work. let me know if you have any questions about it and I'll try and explain :) if you wouldn't mind ticking the question as answered (check mark next to my answer) I would greatly appreciate it! :)
0

You could use a while loop like so:

...
var promptText = "Your text";
var input = prompt(promptText);
while(input !== "1" && input !== "-1"){
  input = prompt("Your answer was not correct\n"+promptText);
}

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.