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 trialrehan sarwar
2,224 PointsAlternate answer
// declare program variables
let num1; let num2; let multiply; let addition; let devide; let subtract; const main = document.querySelector("main")
alert("Let's do some math!");
num1 = prompt("Please type a number");
//loop one for number
while (isNaN(num1) || num1 === ""){ num1 = prompt("That was not a number please type a number"); }
//loop two for number
num2 = prompt("Please type a second number that is not 0");
while (isNaN(num2) || num2 === "" || num2 === "0"){ num2 = prompt("That was either not a number or the number 0. Please type a number that is not 0"); }
//maths
addition = (+num1) + (+num2); multiply = (+num1) * (+num2); subtract = (+num1) - (+num2); devide = (+num1) / (+num2);
//statement
main.innerHTML =
<h2>Math with numbers ${num1} and ${num2}</h2>
<p>${num1} + ${num2} = ${addition}</p>
<p>${num1} * ${num2} = ${multiply}</p>
<p>${num1} - ${num2} = ${subtract}</p>
<p>${num1} / ${num2} = ${devide}</p>
;
Any way of making the above simpler? I've added loops rather than the if statements so that we get an answer.