Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I need prompt the visitor for an integer between 1 and 100 and to continue prompting until a valid number is entered.

Here is what I have:

<script>

var number = parseInt(prompt("Please enter a number from 1 to 100", ""));

if (number < 100) {
    document.write("Your number (" + number + ") is matches requirements", "");
} else if (isNaN(number)) {
    parseInt(prompt("It is not a number. Please enter a number from 1 to 100", ""));
} else {
    parseInt(prompt("Your number (" + number + ") is above 100. Please enter a number from 1 to 100", ""));
}

</script>

It recognizes the number but fails to re-ask when the number is wrong. Can you please help me and explain what you added?

Thank you very much.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
382 views
Welcome To Ask or Share your Answers For Others

1 Answer

Something like this should do the trick:

do{
    var selection = parseInt(window.prompt("Please enter a number from 1 to 100", ""), 10);
}while(isNaN(selection) || selection > 100 || selection < 1);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...