I created a simple prompt
that allows a user to input a number between 1 and 10. First I change answer
from a string into a number. Second I check to see if answer
is between 1 and 10 or is NaN
. If answer
is outside 1 and 10 or NaN
the loop runs and checks again. while
correctly detects if numbers are less than 1 or greater than 10 however it doesn't properly detect if answer == NaN
. I'm not sure why it doesn't work with letters because the alert
indicates that answer does = NaN
when I input letters. Can anybody see what I'm doing wrong?
var answer;
do {
answer = prompt("Enter a number between 1 and 10.");
answer = Number(answer);
}
while (answer < 1 || answer > 10 || answer == NaN);
alert(answer);