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
Renaud is Not Bill Gates
2,14038 gold badges115 silver badges215 bronze badges
2 Answers 2
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
Esailija
140k24 gold badges280 silver badges328 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
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
rationalboss
5,3973 gold badges32 silver badges50 bronze badges
1 Comment
GottZ
i would prefer Esailija's answer
lang-js