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

JavaScript The Solution

Kirok James
seal-mask
.a{fill-rule:evenodd;}techdegree
Kirok James
Full Stack JavaScript Techdegree Student 876 Points

Is is possible to do it for all inputs differently using what we have learned so far?

I was trying to do it for all options, But this code didn't work. Please help

var num1 = "";
var num2 = "";
var message = "";


alert("Let's do some math!");


num1 = prompt("Please type a number");
num1 = parseFloat(num1);

num2 = prompt("Please type another number");
num2 = parseFloat(num2);

if(num2 === 0){
alert("The second number is 0. You can't divide by zero. Reload and try again.")
}else if(num1 === 0){
alert("The first number is 0. You can't divide by zero. Reload and try again.")

}else if(isNan(num1)){
alert("The first number is not a number. Reload and try again.")

}else if(isNaN(num2)){
alert("The second number is not a number. Reload and try again.")

}else{

message = "<h1>Math with the numbers " + num1 + " and " + num2 + "</h1>";
message += num1 + " + " + num2 + " = " + (num1 + num2);
message += "<br>";
message += num1 + " * " + num2 + " = " + (num1 * num2);
message += "<br>";
message += num1 + " / " + num2 + " = " + (num1 / num2);
message += "<br>";
message += num1 + " - " + num2 + " = " + (num1 - num2);

document.write(message);
}

1 Answer

Steven Parker
Steven Parker
229,582 Points

One thing I noticed is that it is not necessary to test the first number for 0, since it is perfectly OK for the numerator to be 0 in division.

Otherwise, this code seems to work, exactly which "options" does it not handle?

And was my answer to your previous question helpful? I noticed you didn't use it here.