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

Conditional Statement. I am keep getting 5 answers correct even though I type wrong answer on purpose.

Here is my code...

var correctAnswer = 0;

var question1 = prompt("What is my name?");

if( question1.toUpperCase() === "JOHN" ) {
    correctAnswer = correctAnswer + 1;
}

var question2 = prompt("What is my father's name?");

if( question2.toUpperCase() === "DAVID" ) {
    correctAnswer = correctAnswer + 1;
}

var question3 = prompt("What is my mother's name?");

if( question3.toUpperCase() === "SARA" ) {
    correctAnswer = correctAnswer + 1;
}

var question4 = prompt("What is my sister's name?");

if( question4.toUpperCase() === "Susan" ) {
    correctAnswer = correctAnswer + 1;
}

var question5 = prompt("What is my dog's name?");

if( question5.toUpperCase() === "POOPOO" ) {
    correctAnswer = correctAnswer + 1;
}

if( correctAnswer = 5 ) { document.write("You have 5 questions correct so you get Gold Crown");

} else if( correctAnswer === 4 || correctAnswer === 3 ) {

document.write("You have " + correctAnswer + " answers correct so you get a silver crown.");

} else if( correctAnswer === 2 || correctAnswer === 1 ) {

document.write("You have " + correctAnswer + " answers correct so you get a bronze crown.");

} else { document.write("You don't get any crown because you have 0 answers correct."); }

1 Answer

Hello SeHyun,

first and foremost, next time you want to ask a question try using proper codeblock wrapping. You can get all the info in the markdown cheatsheet that's visible right under the text editor you're using to post on this forum. Using this right kind of formatting makes your code a lot easier to read and makes spotting problems a lot faster for people who want to help you.

Now back to your problem. You've made a small mistake in your if statement condition:

if( correctAnswer = 5 ) { document.write("You have 5 questions correct so you get Gold Crown");

the single "=" sign is used for assigning variables and it can't be invoked as a logical operator. So you want to use "===" here just like you did in all your previous if statements. You've just got to be really mindful of using proper operators, because they are easy to confuse at the beginning of your coding language learning process.

To reinforce your knowledge, I'd suggest reading this MDN article on logical operators in JavaScript.

Best of luck,

Wiktor Bednarz

Thank you!