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

Can 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');

/*

  1. 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; }

/*

  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
Blake Larson
12,932 Points

It'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>`;

Gotcha. Thanks!