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 trialEmanuel Rouse
1,985 PointsMy conditional statement isn't running due to the isNaN method. Please help.
My conditional statement follows what Dave shows.
if (isNaN(num2)){
alert('Cannot divide by 0. Please reload and try again.');
}
However the code block isn't running. I've managed to get it to work by doing this:
if (isNaN(num2 / 0) {
alert('Cannot divide by 0. Please reload and try again.');
}
But I'm not sure why this works. Can someone explain this to me? I've watched a few videos on isNaN and also checked the MDN docs on it however it's still going over my head.
2 Answers
Bartlomiej Pajak
5,063 PointsIt should be more like this:
if ( num2 === 0 ) {
alert("The second number is 0. You can't divide by zero. Reload and try again.");
}
if ( isNaN(num1) || isNaN(num2) ) {
alert("At least one of the values you typed is not a number. Reload and try again.");
}
KRIS NIKOLAISEN
54,971 PointsBased on your alert it looks like you are testing num2 = 0. If that is the case then your conditional statement is false because 0 is a number. If you try a word on the other hand like 'beaver' or leave it blank then the statement is true and the alert is displayed.