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 JavaScript Basics (Retired) Making Decisions with Conditional Statements The Conditional Challenge

Yuliya Varanitsa
Yuliya Varanitsa
4,783 Points

Does 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
Steven Parker
231,271 Points

The "===" 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
seal-mask
.a{fill-rule:evenodd;}techdegree
Adrian Dzienisik
Full Stack JavaScript Techdegree Student 5,780 Points

prompt() 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!