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 Making Decisions in Your Code with Conditional Statements The Conditional Challenge Solution

Brendan O'Connor
Brendan O'Connor
4,091 Points

Hi all, i can get mine to show "Gold," but no other ranks--suggestions? Will paste my code below...

/* 
  1. Store correct answers
   - When quiz begins, no answers are correct
*/
let answer1 = 1;
let answer2 = 2;
let answer3 = 3;
let answer4 = 4;
let answer5 = 5;

let answer1guess = prompt("What is 1?");
let answer2guess = prompt("What is 2?");
let answer3guess = prompt("What is 3?");
let answer4guess = prompt("What is 4?");
let answer5guess = prompt("What is 5?");

let message;
let rank;
let finalMessage;

if (answer1 === answer1guess) {
  answer1guess = true;
} else {
  answer1guess = false;
}

if (answer2 === answer2guess) {
  answer2guess = true;
}else {
  answer2guess = false;
}
if (answer3 === answer3guess) {
  answer3guess = true;
}else {
  answer3guess = false;
}
if (answer4 === answer4guess) {
  answer4guess = true;
}else {
  answer4guess = false;
}
if (answer5 === answer5guess) {
  answer5guess = true;
}else {
  answer5guess = false;
}

// 2. Store the rank of a player

if (answer1guess) {
  message = 1;
} else if (answer1guess && answer2guess) {
  message = 2;
} else if (answer1guess && answer2guess && answer3guess) {
  message = 3;
} else if (answer1guess && answer2guess && answer3guess && answer4guess) {
  message = 4;
} else if (answer1guess && answer2guess && answer3guess && answer4guess && answer5guess) {
  message = 5;
} 

// 3. Select the <main> HTML element


/*
  4. Ask at least 5 questions
   - Store each answer in a variable
   - Keep track of the number of correct answers
*/



/*
  5. Rank player based on number of correct answers
   - 5 correct = Gold
   - 3-4 correct = Silver
   - 1-2 correct = Bronze
   - 0 correct = No crown
*/

if (message = 5) {
  rank = "Gold";
} else if (message = 3 || 4) {
  rank = "Silver";
} else if (message = 1 || 2) {
  rank = "Bronze";
} else {
  rank = "No crown";
}

// 6. Output results to the <main> element
/*
Ex: "You got X out of 5 questions correct.
Crown earned: X"
*/

finalMessage = `<p>You got ${message} out of 5 questions correct.
Crown earned: ${rank}</p>`; 
document.querySelector("main").innerHTML = finalMessage;

2 Answers

Steven Parker
Steven Parker
216,175 Points

Note that a single "=" is an assignment, not a comparison. So "message" will always be 5 after this:

if (message = 5) {
  rank = "Gold";

Also note that "prompt" always returns a string, so a type-sensitive comparison like this one will always be false:

if (answer1 === answer1guess) {

Finally, note that if the first test in an "else if" chain is true, no other tests will be done:

if (answer1guess) {
  message = 1;

So when answer 1 is correct, the count will always be 1. You will probably want to add up the count before testing it, since the first answer could be wrong but others might be right.

Brendan O'Connor
Brendan O'Connor
4,091 Points

Thanks so much Steven Parker , that's a huge help!

I'll have to do some more work on this and see how it comes out...