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.

asked Oct 20, 2012 at 10:25

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);
answered Oct 20, 2012 at 10:29
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>
answered Oct 20, 2012 at 10:28

1 Comment

i would prefer Esailija's answer

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.