javascript prompt number and continue prompting if answer is wrong
Asked Answered
E

6

9

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.

Encourage answered 23/2, 2013 at 23:56 Comment(0)
B
12

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);
Boren answered 24/2, 2013 at 0:2 Comment(1)
Add a radix parameter of 10 to the parseInt function and you're good to go :)Thistledown
B
2

Here's a recursive approach:

var number = (function ask() {
  var n = prompt('Number from 1 to 100:');
  return isNaN(n) || +n > 100 || +n < 1 ? ask() : n;
}());
Bearberry answered 24/2, 2013 at 0:9 Comment(1)
Better avoid recursion where not necessary. A loop like in this answer https://mcmap.net/q/1155321/-javascript-prompt-number-and-continue-prompting-if-answer-is-wrong is simpler - a good thing!Unskillful
L
1

Another approach:

<html>
    <head> </head>
    <body onload="promptForNumber();">


<script>
    function promptForNumber( text)
{
    if(text == '' ){
     text = "Please enter a number from 1 to 100";   
    }
    var number = parseInt(window.prompt(text, ""));
    checkNumber(number);

}
function checkNumber(number){

    if (number <= 100 && number >= 1) {
    document.write("Your number (" + number + ")  matches requirements", "");
} else if (isNaN(number)) {
    promptForNumber("It is not a number. Please enter a number from 1 to 100", "");
} else {
    promptForNumber("Your number (" + number + ") is not between 1 and 100", "");
}

}


</script>

    </body>
</html>
Loading answered 24/2, 2013 at 0:15 Comment(0)
F
0
 function myFunction(id) {

        let person = prompt("Please Enter Your Quotation");
        if (person != null) {
            if(person>=0||person<0){
                alert("max");
            }else{
            alert('Only Number is Allowed');
            myFunction(id)
            }
        }
    }
Farad answered 23/12, 2021 at 9:37 Comment(1)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Patiencepatient
F
0
    age = prompt("Enter your age:");
    while(isNaN(age)){
        age = prompt("Enter your age in numerals only:");
    };
    function check(age){
        if(age>=21 && age<=80){
            document.write("You can vote.");
        }else if(age>=81){
            document.write("We'll come home to take your vote.");
        }else{
            document.write("You can not vote.");
        };
        return(age);
    }; 
    console.log(check(age));
Flaherty answered 25/2, 2023 at 20:10 Comment(0)
T
0
 const handleCommissionRate = (id: number) => {
        const rate = prompt('Komisyon oranını girin') as unknown as number
        if (isNaN(rate)) {
            return handleCommissionRate(id)
        }
        console.log(id)
    }

this is my example function

Tade answered 8/3, 2023 at 22:19 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.