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 trialZiyu Zhang
Front End Web Development Techdegree Student 7,739 PointsI don't understand why my code doesn't work
Here is my code:
function typeNumber(){
alert("Let's do some math!");
var num1 = prompt("Please type a number");
num1 = parseFloat(num1);
var num2 = prompt("Please type another number");
num2 = parseFloat(num2);
var message;
if(num2 === 0){
alert('The second number is 0. You can\'t divide by zero. Reload and try again.');
} else if (isNan(num1)){
alert('At least one of the values you typed is not a number. Reload and try again.');
} else{
message =`${num1} + ${num2} = ${num1 + num2}`;
document.write(message);
}
// build an HTML message
typeNumber();
1 Answer
Thayer Y
29,789 PointsHi, You need to fix 2 things
1- instead of (isNan(num1)
make it like this (isNaN(num1)
2- you need one more curly brace } to close your function
Ziyu Zhang
Front End Web Development Techdegree Student 7,739 Pointsyes, it works! Thanks Thayer
Brandon White
Full Stack JavaScript Techdegree Graduate 34,662 PointsBrandon White
Full Stack JavaScript Techdegree Graduate 34,662 PointsIn what way is your code not working, Ziyu?
Is it not writing anything to the document object at all, or is it not writing what youβre expecting. If itβs the former, you might take a look at the Document.write method on mdn:
https://developer.mozilla.org/en-US/docs/Web/API/Document/write