1

I want to create a script that when I write a string to check if this string is a number or no if it isn't a number it should give me the input dialogue again, the is the code I tried:

<script>

var nombre;
nombre = parseInt(prompt("Donnez un nombre entre 0 et 999: "));
var nombreIsInt = false;

while(!nombreIsInt)
{
    if(isNaN(nombre))
        prompt("Svp Saisie un nombre entre 0 et 999: ");
    else
        nombreIsInt = true;
}

</script>

The problem is that when I write a number it gives me the input dialogue again.

2 Answers 2

5

Try a do-while loop:

do {
    var nombre = parseInt(prompt("Donnez un nombre entre 0 et 999: "));
    var nombreIsInt = !isNaN(nombre);
} while (!nombreIsInt);
Sign up to request clarification or add additional context in comments.

Comments

3

You need to assign the prompt to nombre. Here:

<script>

var nombre;
nombre = parseInt(prompt("Donnez un nombre entre 0 et 999: "));
var nombreIsInt = false;

while(!nombreIsInt)
{
    if(isNaN(nombre))
        nombre = prompt("Svp Saisie un nombre entre 0 et 999: "); // the problem is here
    else
        nombreIsInt = true;
}

</script>

1 Comment

i would prefer Esailija's answer

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.