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

Ruby Bassi
Ruby Bassi
3,873 Points

Hi, my code works but i was wondering if it could be be shortened / made simpler?

alert("Let's do some math!"); var input1 = parseFloat(prompt("type a number")); var input2 = parseFloat(prompt("type another number")); var addition = input1 + input2; var multiply = input1 * input2; var division = input1 / input2; var subtraction = input1 - input2; var message = "<h1>Math with the numbers " + input1 + " and " + input2 + "</h1>"; message += "<br>" + input1 + " + " + input2 + " = " + addition + "<br>" + input1 + " * " + input2 + " = " + multiply + "<br>" + input1 + " / " + input2 + " = " + division + "<br>" + input1 + " - " + input2 + " = " + subtraction; document.write(message);

2 Answers

nico dev
nico dev
20,364 Points

Another (relatively new) JS concept that you would be stunned with, and you should definitely check (now or later on) is Template Literals, which would allow you to do it just like this:

alert("Let's do some math!");
const firstNum = parseInt(prompt('Give me a number!'));
const secondNum = parseInt(prompt('Now another one!'));
let message = `<h1>Math with the numbers ${firstNum} and ${secondNum}</h1>
<p>${firstNum} + ${secondNum} = ${firstNum + secondNum}</p>
<p>${firstNum} * ${secondNum} = ${firstNum * secondNum}</p>
<p>${firstNum} / ${secondNum} = ${firstNum / secondNum}</p>
<p>${firstNum} - ${secondNum} = ${firstNum - secondNum}</p>`;
document.write(message);

I thought this would be an interesting place to apply it.

Matthew Caloger
Matthew Caloger
12,903 Points

Using functions would greatly help here.

alert("Let's do some math!"); 
var input1 = parseFloat(prompt("type a number")); 
var input2 = parseFloat(prompt("type another number")); 
document.write("<h1>Math with the numbers " + input1 + " and " + input2 + "</h1>"); 
addition(input1, input2);
multiplication(input1, input2);
division(input1, input2);
subtraction(input1, input2);

function addition (num1, num2) {
    var result = num1 + num2;
    printResult(num1, num2, "+", result);
}

function multiplication (num1, num2) {
    var result = num1 * num2;
    printResult(num1, num2, "*", result);
}

function subtraction (num1, num2) {
    var result = num1 - num2;
    printResult(num1, num2, "-", result);
}

function division (num1, num2) {
    var result = num1 / num2;
    printResult(num1, num2, "/", result);
}

function printResult(num1, num2, operationCharacter, result){
    document.write("<br>" + num1 + " " + operationCharacter + " " + num2 + " = " + result);
}

We calculate each value in the 4 operation functions, then we feed that to "printResult" along with the operation character ("+", "-", "*", "/") which formats it for us.

Note that while it may feel unnecessary to pass input1 and input2 each time when we could just refer to it, you really want to avoid using global variables like that whenever possible to avoid confusion in larger programs.