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

I cant get my code to add the number of correct answers, it always says zero

/*

  1. Store correct answers
    • When quiz begins, no answers are correct */ let CorrectAnswer = ;

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

/*

  1. Rank player based on number of correct answers
    • 5 correct = Gold
    • 3-4 correct = Silver
    • 1-2 correct = Bronze
    • 0 correct = No crown */ const FirstQuestion = prompt("What is my first name"); if (FirstQuestion.toUpperCase === 'RODRIGO'){ CorrectAnswer += 1; } const SecondQuestion = prompt("What is my second name"); if (SecondQuestion.toUpperCase === 'ALONSO'){ CorrectAnswer += 1; } const ThirdQuestion = prompt("What is my first last name"); if (ThirdQuestion.toUpperCase === 'GUERRERO'){ CorrectAnswer += 1; } const FourthQuestion = prompt("What is my second last name"); if (FourthQuestion.toUpperCase === 'PAQUOT'){ CorrectAnswer += 1; } const FifthQuestion = prompt("What is my age"); if (FifthQuestion === 23){ CorrectAnswer += 1; }

if(CorrectAnswer === 5){ rank = "Gold"; } else if (CorrectAnswer >= 3){ rank = "Silver"; } else if (CorrectAnswer >= 2){ rank = "Bronze"; } else { rank = "None"; }

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

main.innerHTML = <h2> You guessed ${CorrectAnswer} out of 5 </h2> <h1> You have received the ${rank} Crown </h1>;

1 Answer

HI Rodrigo!

You are very close, but there are a few issues:

1) You never initialized CorrectAnswer.

Use this:

let CorrectAnswer = 0; // Step 1

2) In all your if statements use FirstQuestion.twoUpperCase() // It's a function, not a property (and SecondQuestion.twoUpperCase(), etc...)

(For the fifth answer use '23' - prompt returns a string.)

Or use if (parseInt(FifthQuestion) === 23) { //Do stufff }

3) Clean up the main.innerHTML statement...

This works:

main.innerHTML = `<h1> You guessed ${CorrectAnswer} out of 5<br />You have received the ${rank} Crown </h1>`;

Note: for a temperate literal, you need the backticks

Copy this:

<!DOCTYPE html>
<html>
<body>

<main></main>

<script>
/*

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

/*

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

Rank player based on number of correct answers
5 correct = Gold
3-4 correct = Silver
1-2 correct = Bronze
0 correct = No crown */ 
const FirstQuestion = prompt("What is my first name");
console.log(FirstQuestion);
if (FirstQuestion.toUpperCase() === 'RODRIGO'){ CorrectAnswer += 1; }
console.log(CorrectAnswer);

const SecondQuestion = prompt("What is my second name");
if (SecondQuestion.toUpperCase() === 'ALONSO'){ CorrectAnswer += 1; }
console.log(CorrectAnswer);

const ThirdQuestion = prompt("What is my first last name");
if (ThirdQuestion.toUpperCase() === 'GUERRERO'){ CorrectAnswer += 1; }
console.log(CorrectAnswer);

const FourthQuestion = prompt("What is my second last name");
if (FourthQuestion.toUpperCase() === 'PAQUOT'){ CorrectAnswer += 1; }
console.log(CorrectAnswer);

const FifthQuestion = prompt("What is my age");
if (FifthQuestion === '23'){ CorrectAnswer += 1; }
console.log(CorrectAnswer);

if(CorrectAnswer === 5){ rank = "Gold"; } else if (CorrectAnswer >= 3){ rank = "Silver"; } else if (CorrectAnswer >= 2){ rank = "Bronze"; } else { rank = "None"; }

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

main.innerHTML = `<h2> You guessed ${CorrectAnswer} out of 5<br />You have received the ${rank} Crown </h1>`;

</script>

</body>
</html>

Paste it in the left pane here:

https://www.w3schools.com/js/tryit.asp?filename=tryjs_array

And run it.

I hope that helps.

Stay safe and happy coding!