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 trialCarl Axen
4,526 PointsSimpler Method HERE!
alert("Let's do some math!");
let userNumber = prompt("Enter a number");
userNumber = +userNumber;
let secondUserNumber = prompt("Type in another number");
secondUserNumber = +secondUserNumber;
let message = `<h1>Math with numbers ${userNumber} & ${secondUserNumber}</h1>`;
let addition = userNumber + secondUserNumber;
message += `${userNumber} + ${secondUserNumber} = ${addition}`;
let multi = userNumber * secondUserNumber;
message += `${userNumber} * ${secondUserNumber} = ${multi}`;
let division = userNumber / secondUserNumber;
message += `${userNumber} / ${secondUserNumber} = ${division}`;
let subtract = userNumber - secondUserNumber;
message += `${userNumber} - ${secondUserNumber} = ${subtract}`;
document.write(message);
1 Answer
Charlie Bird
7,277 PointsHi Carl,
I like your method, it's similar to what I used.
You can actually make it even simpler by using +prompt("Enter a number");
- by putting the + before the prompt it transforms the input from string to number straight away.
Also, you can do calculations within the template literals used for your message. For example rather than having to declare addition as a separate variable you can do:
message += `${userNumber} + ${secondUserNumber} = ${userNumber + secondUserNumber}`;
Hope you find that useful/interesting, happy coding!
Autumn Rhodes
925 PointsAutumn Rhodes
925 PointsThis response was an excellent trick, which I IMMEDIATELY put to use. Thank you!