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 trialsimsima
Courses Plus Student 2,560 PointsSolution for Math Challenge
// 1. Attach this file -- math.js -- to the index.html file using a <script> tag
// 2. Add an alert to announce the program with a message like "Let's do some math!"
alert(`Let's do some math!`);
// 3. Create a variable and use the prompt() method to collect a number from a visitor
let num1 = prompt(`What is the first number ?`);
let num2 = prompt(`What is the second number ?`)
// 4. Convert that value from a string to a floating point number
let num1p = parseFloat(num1);
// 5. Repeat steps 3 and 4 to create a second variable and collect a second number
let num2p = parseFloat(num2);
// 6. Create a new variable -- message -- which you'll use to build
// a complete message to print to the document
// Start by creating a string that includes <h1> tags as well
// and the two input numbers. The string should look something like this:
// "<h1>Math with the numbers 3 and 4</h1>" where the two numbers are
// the values input from the user. Use string concatenation to create this
// and make sure you actually perform the math on the values by
// using the + symbol to add their values together
message = `<h1> We will be using math operators on the two numbers of ${num1p} and ${num2p}</h1>
<h3> ${num1p} + ${num2p} = ${num1p + num2p} </h3>
<h3> ${num1p} - ${num2p} = ${num1p - num2p} </h3>
<h3> ${num1p} / ${num2p} = ${num1p / num2p} </h3>
<h3> ${num1p} * ${num2p} = ${num1p * num2p} </h3>
`;
// 7. Add another string to the message variable.
// The string should look something like this after concatenation:
// "3 + 4 = 7"
// 8. Add a linebreak tag -- <br> -- to the message variable
// 9. Continue to add to the message variable to include strings
// demonstrating multiplication, division and subtraction
// For example:
// "3 * 4 = 12"
// "3 / 4 = 0.75"
// "3 - 4 = -1"
// 10. Use the document.write() method to print the message variable
// to the web page. Open the finished.png file in this workspace
// to see what the completed output should look like
document.write(message);
Should i have used var instead and is template literal the best way to right the message with out <br>
?
1 Answer
Steven Parker
231,275 PointsFor declaration, "let" is the more modern form and is generally preferable except where scope or hoisting needs make "var" advantageous. Neither of those apply here, so "let" is a good choice. But "const" is an even better choice when the value is not going to be changed later in the program.
Any heading element (like h3) is displayed in block mode, so it will be on a line by itself without using br. This is true no matter what method you output it with. Still, even though it deviates from the commented instructions, the single template literal makes the code compact and easy to read.
simsima
Courses Plus Student 2,560 Pointssimsima
Courses Plus Student 2,560 Pointsthank you trying to learn consistently day by day.
Do you have any articles or youtube videos which can elaborate more on how and what use-case would use the "var" , let or const keyword ??
Steven Parker
231,275 PointsSteven Parker
231,275 PointsI'm sure any search engine will point you to several discussions on keyword choice, but for beginners, "const" will usually be the best choice for anything that won't be changed, and "let" for anything that will. They are both recent additions to the language, so you'll still see "var" used in older lessons.