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

Correct answers aren't properly counted

When calculating the correct answers in order to set the ranking of the player, I am always getting Gold rank even if I deliberately put some wrong answers. I've narrowed it down to the ranking bit of the solution but still can't seem to pinpoint where the problem is. Here's my code on that specific bit.

const question1 = prompt('What is 1 + 1?'); if (answer = 2) { correctAnswer +=1; } const question2 = prompt('What is 2 + 1?'); if (answer = 3) { correctAnswer +=1; } const question3 = prompt('What is 3 + 1?'); if (answer = 4) { correctAnswer +=1; } const question4 = prompt('What is 4 + 1?'); if (answer = 5) { correctAnswer +=1; } const question5 = prompt('What is 5 + 1?'); if (answer = 6) { 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) { console.log(Rank Gold); rank = 'Gold';

} else if (correctAnswer >= 3 ) { console.log(Rank is Silver); rank = 'Silver';

} else if (correctAnswer >=2 ) { console.log(Rank is Bronze); rank = 'Bronze';

} else {
console.log(Rank is no rank); rank = 'No rank'; }

1 Answer

Hi Ronny,

Based on the code you’ve provided, your bug seems to be caused by the fact that you’re using an assignment operator as if it were an equality operator.

In your if conditions, you want to use ===, which means strictly equal, not = which means assign the value on the right, to the variable on the left.

That said, it appears that you’ve only posted a portion of your code (so I can’t tell if there are errors in any of the code that’s not posted).

Hi, Brandon. Thanks for the reply. This was the only bit that I had an issue hence the code snippet. I changed the code and it worked, thanks for the assist.