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 trialjoo
Python Development Techdegree Graduate 16,498 PointsPlease check my solution:
My code works, but please let me know if there's anything that may cause ERROR if I decided to continue working on this file:
// Math alert:
alert("Let's do some math");
// Variables:
var num1;
var num2;
var message;
// Input:
num1 = Number(prompt("Please type the first number"));
num2 = Number(prompt("Please type the second number"));
if (isNaN(num1 && num2)){
alert("At least one of the values you typed is not a number. Reload the page and try again!");
message = false;
}
else if (num2 === 0){
alert("The second number is 0. You can't divide by zero. Reload the page and try again!");
message = false;
}
// HTML Message:
message = "<h1>Math between the numbers " + num1 + " and " + num2 + "</h1><br>";
message += num1 + " + " + num2 + " = " + (num1 + num2) + "<br>";
message += num1 + " - " + num2 + " = " + (num1 - num2) + "<br>";
message += num1 + " * " + num2 + " = " + (num1 * num2) + "<br>";
message += num1 + " / " + num2 + " = " + (num1 / num2) + "<br>";
if (num1 && num2) {
document.write(message);
}
Mod Edit: Fixed code formatting. See Steven Parker's comment below on code-formatting for information on how to do this.
joo
Python Development Techdegree Graduate 16,498 Points// Math alert:
alert("Let's do some math");
// Variables:
var num1;
var num2;
var message;
// Input:
num1 = Number(prompt("Please type the first number"));
num2 = Number(prompt("Please type the second number"));
if (isNaN(num1 && num2)){
alert("At least one of the values you typed is not a number. Reload the page and try again!"); message = false;
}
else if (num2 === 0){
alert("The second number is 0. You can't divide by zero. Reload the page and try again!"); message = false;
}
// HTML Message:
message = "<h1>Math between the numbers " + num1 + " and " + num2 + "</h1><br>";
message += num1 + " + " + num2 + " = " + (num1 + num2) + "<br>";
message += num1 + " - " + num2 + " = " + (num1 - num2) + "<br>";
message += num1 + " * " + num2 + " = " + (num1 * num2) + "<br>";
message += num1 + " / " + num2 + " = " + (num1 / num2) + "<br>";
if (num1 && num2) {
document.write(message);
}
4 Answers
JAYSON RIFFLE
Front End Web Development Techdegree Student 1,887 PointsHave you tried validating it yet? https://jigsaw.w3.org/css-validator/
Steven Parker
231,248 PointsI don't think that service validates JavaScript (just HTML/CSS).
Steven Parker
231,248 PointsHere's a few issues I noticed:
- you can't combine values with logic (but you can use logic to combine comparisons)
- combining with AND (
&&
) means both, for either you'd use OR (||
) - an example of the previous 2 points would be "
if (isNaN(num1) || isNaN(num2)) {
" - setting message to "false" doesn't do anything since it gets assigned with a string next
- the test for 0 is giving the message but not preventing the divide by 0 from happening
- the test of "
num1 && num2
" will return false if either number is 0 - if the numbers fail the criteria, it would be better to prompt again, or trigger a reload by code
Paul May
3,432 PointsIf you want to validate both variables contain numbers in one check you can use something like:
if(isNaN(num1 + num2)){
// trigger the alert
}
since adding a number to NaN results in NaN the alert will be triggered if either of the values are not numbers
Steven Parker
231,248 PointsClever idea ... but your parentheses are unbalanced in this example.
Paul May
3,432 PointsWoops, well spotted, I updated the comment to avoid confusion, thanks for the feedback
Steven Parker
231,248 PointsSteven Parker
231,248 PointsMost of this looks like comments. Use the instructions for code formatting in the Markdown Cheatsheet pop-up below the "Add an Answer" area. Or watch this video on code formatting.