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 Introducing the Practice

Zydrunas Likas
Zydrunas Likas
3,952 Points

My solution please give any advices

alert("Lets do some math");

const number1 = prompt("Choose first number");

const number2 = prompt("Choose second number");

const number1number = +number1;

const number2number = +number2;

const add = number1number + number2number;

const multiply = number1number * number2number;

const divide = number1number / number2number;

const subtract = number1number - number2number;

const message = `<h1> Math with the number ${number1number} and ${number2number} </h1><br>

        <p>${number1number} - ${number2number} = ${subtract}</p><br>

        <p>${number1number} + ${number2number} = ${add}</p><br>

        <p>${number1number} * ${number2number} = ${multiply}</p><br>

        <p>${number1number} / ${number2number} = ${divide}</p>`;

document.write(message);

3 Answers

Hi Zydrunas Likas,

do you have the css for this project? Everything looks ok! If the goal is to output this on the browser then you can add the following line at the end

alert("Lets do some math");

const number1 = prompt("Choose first number");
const number2 = prompt("Choose second number");
const number1number = +number1;
const number2number = +number2;
const add = number1number + number2number;
const multiply = number1number * number2number;
const divide = number1number / number2number;
const subtract = number1number - number2number;

const message = `<h1> Math with the number ${number1number} and ${number2number} </h1><br>
<p>${number1number} - ${number2number} = ${subtract}</p><br>

        <p>${number1number} + ${number2number} = ${add}</p><br>
        <p>${number1number} * ${number2number} = ${multiply}</p><br>
        <p>${number1number} / ${number2number} = ${divide}</p>`;
document.write(message);

Everything looks fine, so what is the question?

Zydrunas Likas
Zydrunas Likas
3,952 Points

Hi, I don't need CSS for this, it's for the JavaScript Math Methods practice solution.

sure! you don't need for this question

Hello Zydrunas!

It sounds like you don't need an answer, but just want to know if there's anything you could have done differently.

You're assigning the numbers provided by the user to a const. This means you can't change them later and increases the number of lines and variable names you have to use at the end.

What I would do here is this:

let firstNumber = prompt("Choose first number")
fisrtNumber = +firstNumber

let secondNumber = prompt("Choose second number")
secondNumber = +secondNumber

What this allows you to do is use the same meaningful name for what the variable represents throughout, because it's only the type of the variable that's changing.

You could make it shorter still and do this:

let firstNumber = +prompt("Choose first number")

let secondNumber = +prompt("Choose second number")

Though the + may get lost by the reader. It's a trade off you and your team would have to decide was worth it or not. Choosing between fewer lines, and potential information getting lost to the reader of the codebase.

Another change I would make is not adding the type information to the variable name. There's a form of variable naming called Hungarian notation where this kind of type information is in the variable name. strName, intAge etc.. It's not unheard of to use this, but it's not common practise.

Lastly I don't think this is a case of common practise and more personal preference - which can come up a lot with different solutions! - and that's that the multiply, subtract... those values are used in just one place. It may be simpler to have the calculations performed inline. Like this:

`${firstNumber} - ${secondNumber} = ${firstNumber - secondNumber}`

Great work!