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

Jason Hill
seal-mask
.a{fill-rule:evenodd;}techdegree
Jason Hill
Full Stack JavaScript Techdegree Student 11,399 Points

Posting to get some feedback

I had to google how to get the two values to add together instead of stringing them together, but the rest I got. Any feedback on more elegance would be appreciated. I see how he did in solution by using +=, wondering if anything else could be improved!

alert("Ready to do some math? Good thing we have a computer we can program to compute!");
var firstNum = prompt("Please enter a number");
parseFloat(firstNum);

var secondNum = prompt("please enter a second number.");
parseFloat(secondNum);
var add = +firstNum + +secondNum;
var sub = firstNum - secondNum;
var mult = firstNum * secondNum;
var div = firstNum / secondNum;

// Message string and math variables
var message = "<h1>Math with the numbers  " + firstNum + " and " + secondNum + "</h1>" + "</br>" +
  firstNum + " + " + secondNum + " = " + add + "</br>" +
  firstNum + " - " + secondNum + " = " + sub + "</br>" +
  firstNum + " * " + secondNum + " = " + mult + "</br>" +
  firstNum + " / " + secondNum + " = " + div;

// Document output
document.write(message);

1 Answer

Steven Parker
Steven Parker
231,248 Points

The "parseFloat" function does not modify the argument, it's value must be assigned (or used directly in an expression).

firstNum = parseFloat(firstNum);

Then, with the prompt strings converted to numbers, you won't need those leading "+" symbols.