You can do it this way:
var numero, menor;
while(numero != 0){
numero = +prompt("Digite um numero: ");
if(numero == 0){
document.write("Menor valor digitado é: ", menor ? menor : "nenhum número foi digitado");
break;
}
if(!menor || numero < menor){
menor = numero;
}
}
Explaining:
The prompt returns a string. Adding the sign of +
before will convert the number typed in type Number
:
+prompt("Digite um numero: ")
You need to do this conversion because otherwise you will compare strings, and "10" is less than "5", for example.
And the prompt should be inside the while
otherwise it will generate an infinite loop, because if a different number of 0
, the while
will run forever, and the prompt will not be called again.
Another thing is to declare the variable menor
worthless, because if it is 1
, no number entered at the prompt will be lower (I am not considering the possibility of entering negative number in your original code).
The second if
checks whether the value of menor
is undefined or less than the number typed. Any of these conditions will reassign the entered value to the variable.
If the user type 0
, will display the message and stop the while
with break
, since it doesn’t matter anymore.
Where is the code?
– Sam
DSCLP I’m new here, I edited my question so you can see the code
– emanuell