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

Kimberly Kohel-Hayes
Kimberly Kohel-Hayes
2,896 Points

Could someone look at my code? I didn't move my let's to the top because that broke the program for some reason.

My program is somewhat different because this is my second program, the first one was the same as his and worked but I wanted to try it like this and I can't get the if statements to even register for some reason.... Thanks!

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

let numberA = prompt("Pick a number.");
numberA = parseFloat(numberA);
console.log(numberA);

let numberB = prompt("Pick a number.");
numberB = parseFloat(numberB);
console.log(numberB);


if ( isNaN(numberB) ) {

    alert("I cannot divide by 0, Please reload and try again!")

}

else if ( numberA != numberA || numberB != numberB ) {

    alert("Please reload and enter numbers!")

}


else {

    let numberAdd = numberA + numberB;
    console.log(numberAdd);
    let numberMult = numberA * numberB;
    console.log(numberMult);
    let numberSubt = numberA - numberB;
    console.log(numberSubt);
    let numberDiv = numberA / numberB;
    console.log(numberDiv);


    message = `<h1>"Math with the numbers ${numberA} and ${numberB}</h1>
                <br>${numberA} + ${numberB} = ${numberAdd}
                <br>${numberA} * ${numberB} = ${numberMult}
                <br>${numberA} - ${numberB} = ${numberSubt}
                <br>${numberA} / ${numberB} = ${numberDiv}
                `;


    document.write(message);

}

4 Answers

Mark Sebeck
MOD
Mark Sebeck
Treehouse Moderator 37,554 Points

Nice job for your second program Kimberly. I formatted your code so it was easier to read. I'm not sure what you are trying to do in the 'if' statements. 'isnan' checks if its not a number. So that works. Not sure what you are trying to check in the else. numberA will always equal numberA. So numberA != numberA will always be false.

If you want to check for 0 you just need

if (numberB == 0) {

}
Kimberly Kohel-Hayes
Kimberly Kohel-Hayes
2,896 Points

I was checking for 0 in the first if and letters in the second if. I was reading MDN at the following link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/isNaN under "Confusing and Special Case Behavior" which made me think that I should write it the way that I did. I will go back to the if (numberB === 0) and else if ( isNaN(numberA) || isNaN(numberB)). I was just trying to figure it all out as if I hadn't watched the video... Thanks!!

Mark Sebeck
MOD
Mark Sebeck
Treehouse Moderator 37,554 Points

Awesome! OK so you taught me something here. For the special cases you need to use Number.isNaN() or

numberA = Number(numberA);

if ( numberA != numberA) {
  alert("Please reload and enter numbers!");
}

so I guess the theory here is that you cast it as a number first. If it's not a number then they won't be equal and if they are numbers they will be equal. So 5 ===5 is true but NaN === NaN is false.

Hello! Any explanation as yo why when declaring the variables at the start the program won't run? Using Inspect I can't tell what is going on. I just had to not declare them at the start and now it works but its a bit frustrating not knowing why :/