Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Josefin Wallgren
909 PointsHow do I make it work?
I have this code:
function hello(){
var age = prompt('How old are you?');
if (age < 20) {
alert('You are younger than 20');
}if (age == 20) {
alert('Nice');
}if (age > 20) {
alert('You are older than 20');
}if (typeof age === "string"){
alert('Type numbers')
}
}
hello();
It was meant to pop up "Type numbers" if you write something else than a number in the field, but it comes up "Type numbers" even if I type a number... What am I doing wrong?
5 Answers
Dino Paškvan
Courses Plus Student 44,108 PointsThis is where things get a bit complicated... Whatever you enter into a prompt is actually a String, even if you type numbers. When you do those comparisons, it gets coerced into a number so that the engine can evaluate the if statement.
You can confirm this by entering this into the JavaScript console:
"19" < 20; // true
"21" < 20; // false
What you should be checking is if the String that gets entered into the prompt can be parsed as a number.
You can do this with the isNaN() function. It will return true if the parameter passed to the function is not a number or a string which can be coerced into a number. That's what NaN stands for — Not a Number.
So, your final if statement would look like this:
if (isNaN(age)) {
alert('Type numbers');
}
Mike Rogers
5,280 PointsAs Andrew points out you could use Regex, but you could also just use an else if for example:
function hello(){
var age = prompt('How old are you?');
if (age < 20) {
alert('You are younger than 20');
}else if (age == 20) {
alert('Nice');
}else if (age > 20) {
alert('You are older than 20');
} else {
alert('Type numbers')
}
}
hello();
Josefin Wallgren
909 PointsThat's awesome! Thanks!
Andrew McCormick
17,730 Pointsi believe the prompt always returns a string. you probably should use Regex to check if the input only contains numbers rather than using typeof
Josefin Wallgren
909 PointsWhat is Regex?
Aleem Mohamed
Courses Plus Student 7,011 PointsI think when it comes to JavaScript prompts whatever you type into the prompt gets returned as a string. Therefore, the way you've structured your code you're always going to evaluate (typeof age === "string") to be true even if you enter a number. I'm fairly new to the programming game so someone more versed can correct me if I'm wrong.
Josefin Wallgren
909 PointsThank you so much for the quick answers! It really helped me understand this! :-)
Josefin Wallgren
909 PointsJosefin Wallgren
909 PointsThanks for the code, now it works great! :)