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 trialBrendan O'Connor
4,091 PointsHi 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
231,268 PointsNote 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
4,091 PointsThanks 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...