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

Tara Edwards
Tara Edwards
1,355 Points

I got everything to work but the message condition, but that isn't included in the video review.

I can't get my message to display when my values are valid.

// declare program variables
var num1 
var num2 
var message
var failure = 0

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

// collect numeric input
num1 = prompt("Please type a number");
num1 = parseFloat(num1); 
num2 = prompt("Please type another number greater than zero");
num2 = parseFloat(num2);

// check for valid input

if (isNaN(num1) || isNaN(num2)) {
    failure += 1;
    alert("One of your values is not a number.  Please reload and try again");

    } 
if (num2 === 0){
   failure += 1;
   alert("value 2 cannot be zero.  Please reload and try again.");

  }

// build an HTML message
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);

// write message to web page
if (failure = 0){
document.write(message);
}

1 Answer

Cory Harkins
Cory Harkins
16,486 Points

Upon first inspection (without testing the code), in your last if statement, you are assigning 0 to failure instead of testing for a boolean value (true/false).

I am assuming that you are testing for "no failure attempts" and if that is true, then write the message. Check your comparison operators.

I would work out better logic though, because if I input a failure, but then carry through with valid responses on the second attempt, it still won't display a message.

Tara Edwards
Tara Edwards
1,355 Points

Thank you! I see now that I should use the === rather a single =.

Since the requirement indicates "Don't build the message variable, or write the message to the doc, if either case 2 or 3 above fails." I think the logic is correct... well except for the "don't build the message variable part" I need to work on that part ..

Thanks again!!