Locked lesson.
About this lesson
Let's make sure the user entered a number!
Exercise files
Download this lesson’s related exercise files.
Checking for Numbers.docx59.4 KB Checking for Numbers - Solution.docx
59.4 KB
Quick reference
Checking for Numbers
Let's check to make sure the user entered a number.
When to use
Use this whenever a user submits an answer.
Instructions
To check whether a thing is a number or not, use isNan, which returns true if an item is not a number.
if (isNaN(userAnswer)) {output = "Hey! That's not a number, try again!";} else {output = userAnswer;}
Hints & tips
- Use isNaN to check whether a thing is a number or not
- isNaN returns true if a thing is NOT a number (true, this is not a number)
Lesson notes are only available for subscribers.