Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript The Solution

Chris Conwell
Chris Conwell
2,268 Points

A different way?

I was excited to see that My solution is almost exactly like the teacher's solution. However a minor difference. IsNaN(num1,num2) Is this proper?

// declare program variables
var num1;
var num2;
var message;

alert("Let's do some math!"); // announce the program

num1 = prompt("Please type a number"); // collect numeric input
num1 = parseFloat(num1); //convert string to float
num2 = prompt("Please type another number");
num2 = parseFloat(num2);


if ( num2 === 0) { // check for zero
  alert("The second number is 0. You can't divide by zero. Reload and try again.");
}

else if (isNaN(num1,num2)){ // Is not a number?
  alert("At least one of the values you typed is not a number. Reload and try again."); 
} else { 
// build an HTML message
  message = "<h1>Math with the numbers " + num1 + " and " + num2 + "</h1>";
  message += num1 + " + " + num2 + " = " + (num1 + num2);  
  message += "<be>";
  message += num1 + " * " + num2 + " = " + (num1 * num2);
  message += "<be>";
  message += num1 + " / " + num2 + " = " + (num1 / num2);
  message += "<be>";
  message += num1 + " - " + num2 + " = " + (num1 - num2);

// write message to web page
document.write(message);
}

6 Answers

Steven Parker
Steven Parker
217,506 Points

The "isNaN" function only tests the first argument, the second one is ignored.

To test two arguments use the "or" operator to combine individual calls :point_right: isNaN(num1) || isNaN(num2)

If you do this as a recap and are familiar with functions and loops you can also combine this:

function numOne(){ num1 = prompt("Please type a number"); num1 = parseFloat(num1) if (isNaN(num1)){ numOne() } return num1 }

function numTwo (){ do { num2 = prompt("Please type another number") num2 = parseFloat(num2)} while (num2===0 || isNaN(num2)) return num2 }

numOne() numTwo()

I ended up using "else if ( isNaN(num1 + num2) )", which does not seem to have been mentioned, but it works either way.

Steven Parker
Steven Parker
217,506 Points

Since doing math on NaN is only one of several reasons an operation might produce NaN, with this test you cannot be certain that either term was NaN to begin with.

I'm confused, then, Steven Parker ; when I ran the test using NaN one way or the other, it operated fine. Am I missing something?

Steven Parker
Steven Parker
217,506 Points

Here's an example:

a = "42";
b = Infinity;
console.log("a is " + (isNaN(a) ? "" : "not ") + "NaN...");
console.log("and b is " + (isNaN(b) ? "" : "not ") + "NaN...");
console.log("but a+b is " + (isNaN(a+b) ? "" : "not ") + "NaN!");

a is not NaN..
and b is not NaN..
but a+b is NaN!   :point_left: :astonished:

This difference is writing programs that work most of the time vs. programs that work all of the time.   :wink:

Steven Parker AHAH! Blind eyes regardless of surgery. Thank you!