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 trialLewis Sheeran
Web Development Techdegree Graduate 19,398 PointsHere's my solution. I tried to make it as lean as possible. I'm sure there's a more concise way.. let me know!
firstNum = prompt("Enter your first number");
secondNum = prompt("Enter your second number");
if ( isNaN(firstNum) || isNaN(secondNum) ) {
alert("Please enter numerical values only");
}
else if ( parseInt(secondNum) === 0 ) {
alert ("You cannot divide by zero. Reload and try again");
}
else {
document.write(`<h1> Math with numbers ${firstNum} and ${secondNum}</h1>`)
document.write(`<p> ${firstNum} + ${secondNum} = ${parseInt(firstNum) + parseInt(secondNum)} </p>`);
document.write(`<p> ${firstNum} x ${secondNum} = ${parseInt(firstNum) * parseInt(secondNum)} </p>`);
document.write(`<p> ${firstNum} / ${secondNum} = ${parseInt(firstNum) / parseInt(secondNum)} </p>`);
document.write(`<p> ${firstNum} - ${secondNum} = ${parseInt(firstNum) - parseInt(secondNum)} </p>`);
}
1 Answer
martinjones1
Front End Web Development Techdegree Graduate 44,824 PointsNice work!
You could use just one call to document.write
like that shown below:
firstNum = prompt("Enter your first number");
secondNum = prompt("Enter your second number");
if ( isNaN(firstNum) || isNaN(secondNum) ) {
alert("Please enter numerical values only");
}
else if ( parseInt(secondNum) === 0 ) {
alert ("You cannot divide by zero. Reload and try again");
}
else {
document.write(`
<h1>Math with numbers ${firstNum} and ${secondNum}</h1>
<p>${firstNum} + ${secondNum} = ${parseInt(firstNum) + parseInt(secondNum)}</p>
<p>${firstNum} x ${secondNum} = ${parseInt(firstNum) * parseInt(secondNum)}</p>
<p>${firstNum} / ${secondNum} = ${parseInt(firstNum) / parseInt(secondNum)}</p>
<p>${firstNum} - ${secondNum} = ${parseInt(firstNum) - parseInt(secondNum)}</p>
`);
}
There is a few other things you could do as well to make it leaner, but honestly, right now I would not sweat it, as you pick these things up over time, you will apply them more and more.