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 trialVaidehi S
5,731 PointsCan you please tell me what is wrong? even if 5 of 5 correct, Its not picking up ranks and doesn't display the Gold.
let correct = 0;
// 2. Store the rank of a player
let rank;
if (correct === 5) { rank = "Gold"; } else if (correct >= 3) { rank = "Silver"; } else if (correct >= 1) { rank = "Bronze"; } else { rank = "None"; }
// 3. Select the <main> HTML element
const main = document.querySelector('main');
/*
- Ask at least 5 questions
- Store each answer in a variable
- Keep track of the number of correct answers */
const Q1 = prompt("There are how many states in USA?");
if (Q1 === "52") { correct += 1; } const Q2 = prompt("There are how many months in a year?");
if (Q2 === "12") { correct += 1; } const Q3 = prompt("Which planet comes before Earth?");
if (Q3.toUpperCase() === "MARS") { correct += 1; } const Q4 = prompt("Is sun a star? true / false");
if (Q4.toUpperCase() === "TRUE") { correct += 1; } const Q5 = prompt ("How many mins in 1 hour?");
if (Q5 === "60") { correct += 1; }
/*
- Rank player based on number of correct answers
- 5 correct = Gold
- 3-4 correct = Silver
- 1-2 correct = Bronze
- 0 correct = No crown */
// 6. Output results to the <main> element
main.innerHTML =
<h1>You got ${correct} out of 5 questions correct.</h1>
<p>You earned ${rank} crown.</p>
;
2 Answers
Blake Larson
13,014 PointsIt's not running the conditional if
block where you are checking correct
after you update the correct
variable. Just put that conditional logic after the prompts.
let correct = 0;
let rank;
const main = document.querySelector('.main'); // ".main"
const Q1 = prompt("There are how many states in USA?");
if (Q1 === "52") { correct++; }
const Q2 = prompt("There are how many months in a year?");
if (Q2 === "12") { correct++; }
const Q3 = prompt("Which planet comes before Earth?");
if (Q3.toUpperCase() === "MARS") { correct++; }
const Q4 = prompt("Is sun a star? true / false");
if (Q4.toUpperCase() === "TRUE") { correct++; }
const Q5 = prompt ("How many mins in 1 hour?");
if (Q5 === "60") { correct++; }
//Checking correct after all prompts.
if (correct === 5)
{ rank = "Gold"; }
else if (correct >= 3)
{ rank = "Silver"; }
else if (correct >= 1)
{ rank = "Bronze"; }
else
{ rank = "None"; }
main.innerHTML = `<h1>You got ${correct} out of 5 questions correct.</h1> <p>You earned ${rank} crown.</p>`;
Vaidehi S
5,731 PointsGotcha. Thanks!