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

Here is my solution. Any comments / thoughts are welcome.

Code

// declare program variables
var num1;
var num2;
var message;

// announce the program
alert("Let's do some math!");


// loop until condition 1 is correct
while(isNaN(num1) || num1 === 0){

  // collect numeric input 1
  num1 = prompt("Please type a number");
  num1 = parseFloat(num1);

    // check input 1
    if(isNaN(num1) || num1 === 0){
      alert("that is not a valid input, try again")
    }
}
// loop until conditoon 2 is correct
while(isNaN(num2) || num2 === 0){

  // collect numeric input 2
  num2 = prompt("Please type another number");
  num2 = parseFloat(num2);
    // check input 2
    if(isNaN(num2) || num2 === 0){
      alert("that is not a valid input, try again")
    }
}

// build an HTML message
message = "<h1>Math with the numbers " + num1 + " and " + num2 + "</h1>";
message += num1 + " + " + num2 + " = " + (num1 + num2);
message += "<br>";
message += num1 + " * " + num2 + " = " + (num1 * num2);
message += "<br>";
message += num1 + " / " + num2 + " = " + (num1 / num2);
message += "<br>";
message += num1 + " - " + num2 + " = " + (num1 - num2);

// write message to web page
document.write(message);

Go ahead and reformat your code so it looks nice in this post. Nobody is going to help if you won't take the time to make your code look nice!

Thanks for the advise Brandon, hope this is what you meant.

2 Answers

Daniel Springer So in JS you can use something called Template Literals to improve code legibility. Instead of using single or double quotations for strings, you can use the backtick, it's the key to the left of the 1 on your keyboard. So your "build an HTML message" portion of the code would go from

before

message = "<h1>Math with the numbers " + num1 + " and " + num2 + "</h1>";

after

message = `<h1>Math with the numbers ${num1} and ${num2}</h1>`;

Less typing, easier to read and variables are easily identifiable by the ${ } that they are placed inside.

That is an awesome trick, thanks for that, I will remember that for sure!

Thanks you very much for that Brandon!

Hey Daniel,

Your code looks good.

Your comments and indentations are great very easy to read!

Thanks for the feedback Colin, much appreciated.