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 trialDylan Carter
1,046 PointsSharing my code, went a little above and beyond with some of the other principles we learned!
Just wanted to share the work i did on this project, in addition to the assignment, i also included and if statement to check if they entered something thats not a number and to change the message accordingly.
Would love to hear any feedback on how you would do it differently or any tips for me!
let messageFalse = false;
alert('Time for some math!');
let visitorNumber1 = prompt('Please enter a number.');
visitorNumber1 = +visitorNumber1;
let visitorNumber2 = prompt('Please enter a second number.');
visitorNumber2 = +visitorNumber2;
if (isNaN(visitorNumber1) || isNaN(visitorNumber2) ) {
alert('Sorry you entered a non numerical character. Please refresh the page to try again');
messageFalse = true;
}
let visitorAddition = visitorNumber1 + visitorNumber2;
let visitorMultiply = visitorNumber1 * visitorNumber2;
let visitorDivide = visitorNumber1 / visitorNumber2;
let message = `<h1> math with the numbers ${visitorNumber1} and ${visitorNumber2}.</h1>`
message += `${visitorNumber1} + ${visitorNumber2} = ${visitorAddition}<br>
${visitorNumber1} * ${visitorNumber2} = ${visitorMultiply}<br>
${visitorNumber1} / ${visitorNumber2} = ${visitorDivide}<br>
`
if (messageFalse === true) {
message = 'Please try again';
}
document.write(message);
1 Answer
Steven Parker
231,248 PointsBe aware that a unary plus is not quite the same thing as calling "parseFloat". For example, passing it an empty string will cause parseFloat to return "NaN", but a unary plus will convert it to 0.
Dylan Carter
1,046 Pointsgood to know ill keep that in mind and add it to my notes
Blake Runyon
Full Stack JavaScript Techdegree Student 7,539 PointsBlake Runyon
Full Stack JavaScript Techdegree Student 7,539 PointsHello! Like your project a lot. Going above and beyond is what will separate you quickly as a new developer.
A few thoughts on what you've created. (I'm also very new :D)
visitorNumber2 = +visitorNumber2;
Prompt actually sets a value for the variable. If you replace the line I've noted before with console.log(visitorNumber2); you can see this.
Could you potentially break it apart for readability?
Again, I'm also very new. So this is helping me learn as well! Thanks for posting!