JavaScript JavaScript Basics Making Decisions in Your Code with Conditional Statements The Conditional Challenge Solution

Vaidehi S
Vaidehi S
4,278 Points

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,905 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>`;
Vaidehi S
Vaidehi S
4,278 Points

Gotcha. Thanks!