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

Ben Dowsett
Ben Dowsett
5,582 Points

Missing something

I have written the following code and cant understand why the console always logs 5Gold no matter the answers

/*

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

let correctAnswer = 0;

// 2. Store the rank of a player let rank = ""

// 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 questionOne = prompt ('What is the capital of England'); if(questionOne === "London");{ correctAnswer +=1;} const questionTwo = prompt ('What is the capital of France'); if (questionTwo === "Paris");{ correctAnswer +=1;}
const questionThree = prompt ('What is the capital of Germany'); if (questionThree === "Berlin");{ correctAnswer +=1;}
const questionFour = prompt ('What is the capital of Italy'); if (questionFour === "Rome");{ correctAnswer +=1;}
const questionFive = prompt ('What is the capital of Spain'); if (questionFive === "Spain");{ correctAnswer +=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 */ if (correctAnswer === 5) { rank= "Gold"; } else if (correctAnswer >=3) { rank= "Silver"; } else if (correctAnswer >=2) { rank= "Bronze"; } else{ rank = "No Crown" }

// 6. Output results to the <main> element console.log (correctAnswer + rank);

4 Answers

Ben Dowsett
Ben Dowsett
5,582 Points
  1. Store correct answers
   - When quiz begins, no answers are correct
*/

let correctAnswer = 0;

// 2. Store the rank of a player
let rank = ""

// 3. Select the <main> HTML element
const main = document.querySelector ('main');

/*
  4. Ask at least 5 questions
   - Store each answer in a variable
   - Keep track of the number of correct answers
*/

const questionOne = prompt ('What is the capital of England');
if(questionOne === "London");{
  correctAnswer +=1;} 
const questionTwo = prompt ('What is the capital of France');
if (questionTwo === "Paris");{
  correctAnswer +=1;}  
const questionThree = prompt ('What is the capital of Germany');
if (questionThree === "Berlin");{
  correctAnswer +=1;}  
const questionFour = prompt ('What is the capital of Italy');
if (questionFour === "Rome");{
  correctAnswer +=1;}  
const questionFive = prompt ('What is the capital of Spain');
if (questionFive === "Spain");{
  correctAnswer +=1;}

/*
  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 (correctAnswer === 5) {
  rank= "Gold";
} else if (correctAnswer >=3) {
  rank= "Silver";
} else if (correctAnswer >=2)  { 
  rank= "Bronze";
} else{
  rank = "No Crown"
}



// 6. Output results to the <main> element
console.log (correctAnswer + rank);

You don't need a semicolon in the conditions -

if(questionOne.toUpperCase() === "London"){ correctAnswer +=1}

plus call toUpperCase or toLowerCase for your strings

And you missing curly bracers at the last part of your code:

if (correctAnswer === 5) { rank= "Gold"}

Conditional statement is following this pattern - if(condition){do something}

Your 2nd and 3rd points are commented with //, you have to assign variables under the instructions. Point 6 will display the answer in console only, try main.innerHTML