So I made a program which is able to check if you are old enough to drive based on the age you type. Here is the code:
const userAge = Number(prompt(`What is your age?`));
if (typeof userAge === NaN) {
console.log('Error - please reload the page and type your age. ¯\_(ツ)_/¯');
} else {
if (userAge >= 18) {
console.log('You can start driving! ');
} else {
console.log(`Sorry - you'll have to wait a bit until you can drive. `)
}
}
I was trying to handle the possibility of someone typing in a string of letters instead of a number, in which case the value of userAge would be NaN when something strange showed up.
When I was checking if the value of userAge was NaN, it would return false even if it was NaN.
I combed through the code to see if I had made a simple mistake, but I couldn't find anything. Would appreciate some help.