Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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!!