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

I am always getting 5 out of 5

On the conditional challenge, my code works nicely, but my score is always coming up as 5. I can't see where I've made the mistake, I believe I am incrementing the code properly, so the issue must be in my final conditional. Here is my code - am I doing something dumb?

var score = 0;
var q1 = prompt('An Odenbarus Rosemarus is a...');
                if (q1.toUpperCase("WALRUS")){
score += 1;
} 
var q2 = prompt('True or False: All walruses have tusks.');
                if (q2.toUpperCase("FALSE") || q2.toUpperCase("F")){
score += 1;
}
var q3 = prompt('True of False: Walruses eat clams.');
                if (q3="true"){
score += 1;
}
var q4 = prompt('Do walruses live with penguins?');
                if (q4="No"){
score += 1;
} 
var q5 = prompt('There are both Atlantic and ___ Walruses.');
                if (q5="Pacific"){
score += 1;
} 

if (score === 5){

    document.write('<p><strong>You got all five questions right!</strong><br />You have acheived a Gold Crown!</p>');
            } else if 
                (score > 2 && score < 5)
            {
            document.write('<p>You have acheived a silver crown!</p>');
            }  else if 
                (score === 1 || score === 2){
                document.write('<p>You have acheived a bronze crown!</p>');
            }

                else {
                    document.write('<p>You answered all questions incorrectly.<br />No crown is awarded.</p>');

}

2 Answers

Steven Parker
Steven Parker
220,925 Points

You're not actually comparing the answers with anything, but you have expressions which generally evaluate as "truthy".

Example:

  if (q1.toUpperCase("WALRUS"))      // this only checks if the answer is not empty
  if (q1.toUpperCase() == "WALRUS")  // but this would test if the answer was "walrus"

Also note that a single equal sign is an assignment, not a comparison.

Of course... I was getting a little crosseyed and trying to do things too quickly. Thanks. And now you know more than you wanted to know about walruses.