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

Can someone check my code?

Can someone check my code? I did not understand why we had to convert the numbers given by the user to floating point number, but I kept going anyway. Also, the quote marks around the HTML were added after looking at the questions of this video. I think there is some duplication that goes against the DRY principles, but I got stucked and could come up with a shorter way to write this code. Alsp, for some reason I could not use the addition operation after the <h1> tag without converting the input again. Any thoughts on that? I would evaluate myself 7/10 for this one!

alert("Let's do some math!");
var inputNumber1 = prompt("Type a number.");
inputNumber = parseFloat(inputNumber1);
var inputNumber2 = prompt("Type another number");
inputNumber2 = parseFloat(inputNumber2);
var message = "<h1>" + "Math  with the numbers " + inputNumber1 + " and " + inputNumber2 + "." + "</h1>";
message += inputNumber1 + " + " + inputNumber2;
message += " = " + (parseFloat(inputNumber1) + parseFloat(inputNumber2));
message += "<br>";
message += inputNumber1 + " * " + inputNumber2;
message += " = " + (parseFloat(inputNumber1) * parseFloat(inputNumber2));
message += "<br>";
message += inputNumber1 + " / " + inputNumber2;
message += " = " + (parseFloat(inputNumber1) / parseFloat(inputNumber2));
message += "<br>";
message += inputNumber1 + " - " + inputNumber2;
message += " = " + (parseFloat(inputNumber1) - parseFloat(inputNumber2));
message += "<br>";
document.write(message);
Adomas Domeika
Adomas Domeika
6,151 Points

you are assigning parseFloat(inputNumber1) to inputNumber, not inputNumber1, (line 3 of your code) that’s why you have to parse them all over again.. fix it and it should work.

1 Answer

Steven Parker
Steven Parker
229,732 Points

The reason the conversion is needed to begin with is that the input from "prompt" is always a string. So to be able to use it for math it must be converted.

But the conversion only needs to be done once, so most of the "parseFloat" calls can be eliminated once you fix the bug spotted by Adomas.

Ah man! The minor mistakes! HAHA! Thanks!

I understand that parseFloat is used to transform a string to a number. I get that. I just didn't get why to use parseFloat. According to the MDN, "the parseFloat() function parses an argument and returns a floating point number."

Why do we need a floating point number in here?

Wouldn't be better to use parseInt? According to the MDN, "the parseInt() function parses a string argument and returns an integer of the specified radix (the base in mathematical numeral systems)."

Isn't this what we want/need for this assignment?

Steven Parker
Steven Parker
229,732 Points

Using "parseFloat" will handle inputs like "2.5", but that would cause an error for "parseInt".