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 Solution

Code isn't calculating score

Hi all,

I've been trying to get the below to work but it seems to be failing at calculating the score (when all answers are correct the score remains zero). Console warns me that 'score' variable has already been declared but I can't see it. Can anyone see my mistake?

Forgive the very boring quiz questions! :D

/*

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

let score = 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 */

let q1 = prompt('What is 1'); let q2 = prompt('What is 2'); let q3 = prompt('What is 3'); let q4 = prompt('What is 4'); let q5 = prompt('What is 5');

if (q1 === 1) {score += 1;} if (q2 === 2) {score += 1;} if (q3 === 3) {score += 1;} if (q4 === 4) {score += 1;} if (q5 === 5) {score += 1;}

alert(score);

/*

  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 (score === 0) { rank = "No crown"; } if (score > 0 && score <= 2) { rank = "bronze"; } if (score > 2 && score <= 4) { rank = "silver"; } if (score === 5) { rank = "gold"; }

// 6. Output results to the <main> element main.innerHTML = <h1>your rank is ${rank}</h1>;

Gary Stevenson

You're answer is correct. Could you move your reply from comments into the answer section? If you do people can mark it as correct and it shows in the forum as answered.

You can add an answer at the very bottom of the page.

Gary Stevenson
Gary Stevenson
2,310 Points

Ross King

Hi.

Thanks for the heads up! Comment moved to answer section.

Cheers, Gary

1 Answer

Gary Stevenson
Gary Stevenson
2,310 Points

Hi.

Sorry I'm just taking this course as a complete beginner, so I haven't got the terminology down yet, but I believe your issue is coming from the fact that your questions are returning the answers as a string, but you are assigning the correct answer as a number.

for example;

The user is prompted to enter the number 1 into the first prompt box and this returns a string.

"(q1 === 1)". This triple-equals is saying that the prompt (q1) should be EXACTLY the same as "1" but the answer to prompt q1 is a string, not a number.

I think your problem will be solved if you change it to;

(q1 === "1") - The quotation marks change the 1 to a string also, which means they are now exactly equal.

Does that make sense?

P.S. The way I suggested would be an easy fix but theres another way also.

Not sure if you have reached this module yet but you can also use the " parseInt() " method too. This function converts a string into a numerical value, so it would convert "1" into 1.

an example would be ( parseInt(q1) === 1) or could be simplified ( +q1 === 1 )

Again, im pretty new to this so I apologize if my explanation is lacking or my terminology is off.

Cheers, Gary

Hi Gary, thanks for coming back to me so quickly - you fixed it :)