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

Duo Liu
Duo Liu
2,036 Points

+= not storing correct number inside variable

Everything works except the number stored inside the "correctAnswer" variable is wrong even when I get all 5 questions right, it will say I only got 2 questions right.

Here is my code:

var correctAnswer = 0;

var name = prompt("What is my name?"); if (name.toUpperCase() === "KATIE"){ correctAnswer += 1; console.log(correctAnswer); }

var age = prompt("What is my age?"); if (age === 20){ correctAnswer += 1; console.log(correctAnswer); }

var color = prompt ("what is my favorite color?"); if ( color.toUpperCase() === "RED"){ correctAnswer += 1; console.log(correctAnswer); }

var month = prompt ("Which month was my birthday?"); if (month.toUpperCase === "AUGUST"){ correctAnswer += 1; console.log(correctAnswer); }

var year = prompt ("which year was I born?"); if (year === 1996){ correctAnswer += 1; console.log(correctAnswer); }

document.write("You have "+correctAnswer+" right answers!")

if (correctAnswer === 5){ document.write ("You get a gold crown!"); }else if (correctAnswer === 4 ){ document.write ("You get a silver crown!"); } else if (correctAnswer === 3 ){ document.write("You get a bronze crown!"); } else { document.write ("You don't get any crowns this time."); }

2 Answers

Stephan Olsen
Stephan Olsen
6,650 Points
var correctAnswer = 0;

var name = prompt("What is my name?");
if (name.toUpperCase() === "KATIE") {
    correctAnswer += 1;
    console.log(correctAnswer);
}

var age = prompt("What is my age?");
if (age === 20) {
    correctAnswer += 1;
    console.log(correctAnswer);
}

var color = prompt("what is my favorite color?");
if (color.toUpperCase() === "RED") {
    correctAnswer += 1;
    console.log(correctAnswer);
}

var month = prompt("Which month was my birthday?");
if (month.toUpperCase === "AUGUST") {
    correctAnswer += 1;
    console.log(correctAnswer);
}

var year = prompt("which year was I born?");
if (year === 1996) {
    correctAnswer += 1;
    console.log(correctAnswer);
}

document.write("You have " + correctAnswer + " right answers!")

if (correctAnswer === 5) {
    document.write("You get a gold crown!");
} else if (correctAnswer === 4) {
    document.write("You get a silver crown!");
} else if (correctAnswer === 3) {
    document.write("You get a bronze crown!");
} else {
    document.write("You don't get any crowns this time.");
}

When you use prompt, it will give you a value of the type string. So in the 2nd and 5th question, your condition is never true, because you're using the === operator, which evaluates if both the value and type is the same. Either you should convert the value of the input to a number, put in the answers as strings, or use the == operator, to just compare the values and not the type.

In your 4th question you forgot the paranthesis on the toUpperCase() function.

Duo Liu
Duo Liu
2,036 Points

Oh I understand it now, thank you!!