admin管理员组文章数量:1022949
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");
}
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");
}
Share
Improve this question
asked Jun 10, 2015 at 21:46
ChiefChief
9631 gold badge15 silver badges34 bronze badges
3
- 1 wrap it in a while loop? while(true){ your code here } – John Ruddell 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. – cHao 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 – Chief Commented Jun 11, 2015 at 10:29
3 Answers
Reset to default 1var 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.
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 e 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
}
}
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);
}
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");
}
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");
}
Share
Improve this question
asked Jun 10, 2015 at 21:46
ChiefChief
9631 gold badge15 silver badges34 bronze badges
3
- 1 wrap it in a while loop? while(true){ your code here } – John Ruddell 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. – cHao 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 – Chief Commented Jun 11, 2015 at 10:29
3 Answers
Reset to default 1var 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.
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 e 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
}
}
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);
}
本文标签:
版权声明:本文标题:javascript - Prompt user for specific input, loop and prompt until correct input is entered - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745540566a2155164.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论