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 trial

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

My code is showing the incorrect rank at the end. Am I using too much code? should I start over?

/* 
  1. Store correct answers
   - When quiz begins, no answers are correct
*/

let firstAnswer = "true";
let secondAnswer = "false";
let thirdAnswer = "true";
let fourthAnswer = "false";
let fifthAnswer = "true";

// 2. Store the rank of a player
let score = 0;

// 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
*/

const questionOne = prompt("true or false? The earth is round.");
  if (questionOne === firstAnswer) {
   console.log("Correct!");
   score++;
} else {
  console.log("Wrong!");
}

const questionTwo = prompt("true or false? dogs are donut shaped.");
  if (questionTwo === secondAnswer) {
    console.log("Correct!");
    score++;
} else {
  console.log("Wrong!");
}

const questionThree = prompt("true or false? there's a skeleton inside you.");
  if (questionThree === thirdAnswer) {
    console.log("Correct!");
    score++;
} else {
  console.log("Wrong!");
}

const questionFour = prompt("true or false? the dinosaurs flew to another planet.");
  if (questionFour === fourthAnswer) {
  console.log("Correct!");
    score++;
  }

const questionFive = prompt("true or false? Michael Jordan played basketball.");
  if (questionFive === fifthAnswer) {
   console.log("Correct!"); 
    score++;
  } else {
   console.log("Wrong!"); 
  }


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

let bronzeRank = score === 1 || score === 2;
let silverRank = score === 3 || score === 4;
let goldRank = score === 5;
let finalRank = score === bronzeRank || score === silverRank || score === goldRank;

if (score === 5) {
 console.log("You got GOLD!");
} else if (score === 3 || score === 4) {
 console.log("You got SILVER!"); 
} else if (score === 1 || score === 2) {
 console.log("You got BRONZE!"); 
} else {
 console.log("You got none of them correct!"); 
}

if (finalRank === goldRank) {
    finalRank = "GOLD!"; 
} else if (finalRank === silverRank){
    finalRank = "SILVER!";
} else if (finalRank === bronzeRank){
    finalRank = "BRONZE!";
} else {
  finalRank = "FAILED TOTALLY!"; 
}

// 6. Output results to the <main> element

document.querySelector("main").innerHTML = `<h1>You got ${score} out of 5 correct!</h1>
                                            Your rank is <h2>${finalRank}</h2>`; 

1 Answer

Steven Parker
Steven Parker
231,236 Points

Your "rank" variables are boolean, for instance:

let goldRank = score === 5;     // this will be either TRUE or FALSE

So when you test them, you won't need to make any comparisons:

if (finalRank === goldRank) {   // instead of this...
if (goldRank) {                 // just do this

And you won't need the first assignment of "finalrank" at all since it will be overwritten with a string before it is used.


Optionally, if you'd like to reduce the amount of code, you could eliminate the individual "rank" variables entirely and combine the two test series into one:

if (score === 5) {
  console.log("You got GOLD!");
  finalRank = "GOLD!"; 
} else if (score === 3 || score === 4) {
  console.log("You got SILVER!"); 
  finalRank = "SILVER!";
} else if (score === 1 || score === 2) {
  console.log("You got BRONZE!"); 
  finalRank = "BRONZE!";
} else {
  console.log("You got none of them correct!"); 
  finalRank = "FAILED TOTALLY!"; 
}

I see! Thank you so much for your help, I had a feeling I may have "over" coded it.