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 trialJohn Bastian Bolhano
4,800 PointsWhy 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
Full Stack JavaScript Techdegree Graduate 71,350 PointsTo 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
4,800 PointsJohn Bastian Bolhano
4,800 PointsThanks, man. I should have thought this.