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 trialYuliya Varanitsa
4,783 PointsDoes not work completely
var correct = 0;
var answer1 = prompt("1+1"); if (answer1 === 2){ correct += 1; } var answer2 = prompt("2+2"); if (answer2 === 4){ correct += 1; } if (correct === 2){ document.write("good"); } else if (correct === 1) { document.write("no bad");
} else { document.write("try again"); }
2 Answers
Steven Parker
231,236 PointsThe "===
" operator is type sensitive. So to match, both sides must have the same value and be the same type. But you are comparing numbers and strings so they can never match.
You can fix this two ways (other than manual conversion): you can use the normal equality test operator ("==
") which will convert types for you, or you could use strings instead of numbers to compare with).
if (answer2 == 4) // this allows type conversion
if (answer2 === "4") // this compares string with string.
Adrian Dzienisik
Full Stack JavaScript Techdegree Student 5,780 Pointsprompt() returns a string value. So you're comparing a string and a int, and because you are using strict mode (===), the comparison will be false. One solution could be: use the parseInt() method:
e.g:
var answer1 = parseInt(prompt('1+1'));
Cheers!
Yuliya Varanitsa
4,783 Pointsthank you!
Yuliya Varanitsa
4,783 PointsYuliya Varanitsa
4,783 Pointsthank you!