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 trialolu adesina
23,007 Pointswhere have i gone wrong
//this program is a quiz that asks 5 questions
//var correct stores the score
var correct = 0
question1=prompt('what is 10 + 10?'); answer1=parseInt(question1) if (answer1==20){ correct+= 1
}
question2=prompt('what is the 10th month of the year?'); answer2=question2.toUpperCase() if (answer2="OCTOBER"){ correct+= 1
}
question3=prompt('what is 7x7?'); answer3=parseInt(question3) if (answer3=49){ correct+= 1
}
question4=prompt('what is the capital of england?'); answer4=question4.toUpperCase() if (answer4="LONDON"){ correct+= 1
}
question5=prompt('what is 9+14?'); answer5=parseInt(question5) if (answer5=23){ correct+= 1
}
//the variable correct is always equals 4 what im i doing wrong alert(correct)
2 Answers
Marcin Czachor
9,641 PointsHey,
You forgot about comparison operators (at least in code given above). Checking if answer is correct requires operators like '==' or '==='. You also forgot about semicolons in if statements. Check code below for comparison. :)
var correct = 0;
var question1,
question2,
question3,
question4,
question5;
var answer1,
answer2,
answer3,
answer4,
answer5;
question1 = prompt('what is 10 + 10?');
question2 = prompt('what is the 10th month of the year?');
question3 = prompt('what is 7x7?');
question4 = prompt('what is the capital of england?');
question5 = prompt('what is 9+14?');
answer1 = parseInt(question1);
answer2 = question2.toUpperCase();
answer3 = parseInt(question3);
answer4 = question4.toUpperCase();
answer5 = parseInt(question5);
if (answer1 === 20) {
correct += 1;
}
if (answer2 === "OCTOBER") {
correct += 1;
}
if (answer3 === 49) {
correct += 1;
}
if (answer4 === "LONDON") {
correct += 1;
}
if (answer5 === 23){
correct+= 1;
}
alert(correct);
Timothy Schmidt
4,806 PointsIn all except the first if
statement, you're using the assignment operator =
instead of one of the equality operators ==
or ===
. This is causing them all to evaluate to true.
Some people avoid this problem by putting the variable second, like this:
if ( 49 == answer3 ) { // do stuff }
This way, if you accidentally use just =
, it will cause an error.