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 Introducing the Practice

John Bastian Bolhano
John Bastian Bolhano
4,800 Points

Why did the type of variable change?

In the multiplication and division the variable seems to work as expected while with addition and substraction the variables seems to be as string resulting it as NaN.

alert("Let's do some math!");
var input1 = parseInt(prompt("Please type a number"));
var input2 = parseInt(prompt("Please type another number"));
console.log(typeof(input1,input2));
var message = "<h1>Math with the numbers " + input1 + " and " + input2 + "</h1>"
            + input1 + " + " + input2 + " = " + input1 + input2 + "<br>"
            + input1 + " - " + input2 + " = " + input1 - input2 + "<br>"
            + input1 + " * " + input2 + " = " + input1 * input2 + "<br>"
            + input1 + " / " + input2 + " = " + input1 / input2 + "<br>";
document.write(message);

1 Answer

Dale Severude
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Dale Severude
Full Stack JavaScript Techdegree Graduate 71,349 Points

To fix your code you need to add parenthesis around your calculations. The addition is concatenating them together as strings. The subtraction is creating a NAN.

(input1 + input2)
(input1 - input2)
John Bastian Bolhano
John Bastian Bolhano
4,800 Points

Thanks, man. I should have thought this.