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

Dylan Carter
Dylan Carter
1,046 Points

Sharing 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);
Blake Runyon
seal-mask
.a{fill-rule:evenodd;}techdegree
Blake Runyon
Full Stack JavaScript Techdegree Student 7,539 Points

Hello! 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)

  • I believe this line is redundancy: 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.

  • Instead of asking the visitor to refresh the page here, you could actually just run another prompt! You'll need to separate the statement though because || will not longer work there.
if (isNaN(visitorNumber1) || isNaN(visitorNumber2) ) {
  alert('Sorry you entered a non numerical character. Please refresh the page to try again');
   messageFalse = true;
}
  • It could just be me, but I found this line a little hard to read:
message += `${visitorNumber1} + ${visitorNumber2} = ${visitorAddition}<br>
${visitorNumber1} * ${visitorNumber2} = ${visitorMultiply}<br>
${visitorNumber1} / ${visitorNumber2} = ${visitorDivide}<br>

Could you potentially break it apart for readability?

message += `${visitorNumber1} + ${visitorNumber2} = ${visitorAddition}<br>`
message += `${visitorNumber1} * ${visitorNumber2} = ${visitorMultiply}<br>`
message += `${visitorNumber1} / ${visitorNumber2} = ${visitorDivide}<br>`

Again, I'm also very new. So this is helping me learn as well! Thanks for posting!

1 Answer

Steven Parker
Steven Parker
231,141 Points

Be 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
Dylan Carter
1,046 Points

good to know ill keep that in mind and add it to my notes