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 trialKrasimir Stamenov
4,743 Pointsthe "correct" variable doesn't update after the if statement. Help, please!
Here's my code. When I run it, everything looks fine, but at the end when I answer correctly to the question, the var correct is still zero. Any ideas how to fix it?
var correct = 0;
//Check if the answer is correct or not. If correct, update the correctAnswer +1.
var question1 = prompt("The capital of Bulgaria is?");
if(question1.toUpperCase === 'SOFIA') {
correct = correct + 1;
}
var question2 = prompt("The capital of Germany is?");
if(question2.toUpperCase === 'BERLIN') {
correct = correct + 1;
}
var question3 = prompt("The capital of Hundary is?");
if(question3.toUpperCase === 'BUDAPEST') {
correct = correct + 1;;
}
var question4 = prompt("The capital of Slovenia is?");
if(question4.toUpperCase === 'LJUBLJANA') {
correct = correct + 1;
}
var question5 = prompt("The capital of Italy is?");
if(question5.toUpperCase === 'ROME') {
correct = correct + 1;
}
// Print the number of the correct answers
document.write("<h1>you got " + correct + " out of 5</h1>");
3 Answers
Camilla Holst
8,876 PointsYou need to define the var correct as 0 in the beginning of your code :)
Samy Basset
11,862 Pointsthe correct value isn't updating because you need to use curly braces: toUpperCase(); Like this:
if(question1.toUpperCase() === 'SOFIA');
Krasimir Stamenov
4,743 PointsThank you so much Samy! That was the problem! Now it works! :)
Samy Basset
11,862 PointsYour welcome!
Frantisek Mede
4,749 PointsQuestion 3 !! The state name is "Hungary" not "Hundary" ;)
Krasimir Stamenov
4,743 Pointsgood point
Krasimir Stamenov
4,743 PointsKrasimir Stamenov
4,743 PointsHello Camilla. Thank you for your answer. Actually it is defined already, but somehow I didn't copy this line above. I have it defined but it still doesn't update the value with += 1
Here is the complete script:
var correct = 0;
Per Karlsson
12,683 PointsPer Karlsson
12,683 PointsAn easy way to check if the variable updates is to use the "Developer Tools" in your browser when running your code.