0
I would like the user to be asked a message so that he or she enters a number, if this number is true, a screen will be displayed stating the number typed.
If the user type a letter, a message will be displayed stating that he has entered an invalid number and to type it again.
But I don’t know how to stay in loop until he enters a real number.
Below I leave my attempts.
var num1 = prompt('digite um numero');
if (isNaN(num1)) {
document.write('numero invalido digite novamente')
}
I made the same code using functions, but I wonder if there’s another way.
Code using functions:
<!DOCTYPE html>
<html>
<body>
<script>
digite();
var num1;
function digite() {
num1 = prompt('digite um numero');
// caso usuario digite um numero inválido //
if (isNaN(num1)) {
alert('numero invalido digite novamente');
digite();
}
}
//caso usuário digite um numero válido //
document.write("o numero digitado foi " + num1)
</script>
</body>
</html>
Thank you. I had doubts about that. I’m glad I found this question.
– Maury Developer