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 Solution

the "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
Camilla Holst
8,876 Points

You need to define the var correct as 0 in the beginning of your code :)

Hello 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;

//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>");
Per Karlsson
Per Karlsson
12,683 Points

An easy way to check if the variable updates is to use the "Developer Tools" in your browser when running your code.

Samy Basset
Samy Basset
11,862 Points

the correct value isn't updating because you need to use curly braces: toUpperCase(); Like this:

if(question1.toUpperCase() === 'SOFIA');

Thank you so much Samy! That was the problem! Now it works! :)

Frantisek Mede
Frantisek Mede
4,749 Points

Question 3 !! The state name is "Hungary" not "Hundary" ;)

good point