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 (Retired) Making Decisions with Conditional Statements The Conditional Challenge Solution

ohtanya
ohtanya
7,768 Points

Can anyone tell me why my code doesn't work? It shows a perfect score no matter what.

Here is my code:

var answerCount = 0;
var rank;

var questionOne = parseInt( prompt('What is 1+1?') ) ;
if (questionOne === 2) {
  answerCount += 1
}
var questionTwo = parseInt( prompt('2+2') );
if (questionTwo === 4) {
  answerCount += 1
}
var questionThree = parseInt( prompt('3+3') );
if (questionThree === 6) {
  answerCount += 1
}
var questionFour = parseInt( prompt('4+4') );
if (questionFour === 8) {
  answerCount += 1
}
var questionFive = parseInt( prompt('5+5') );
if (questionFive === 10) {
  answerCount += 1
}

if (answerCount = 5) {
  rank = 'gold crown!';
} else if (answerCount >= 3) {
  rank = 'silver crown!';
} else if (answerCount >= 1) {
  rank = 'bronze crown!';
} else {
  rank = 'no crown';
}

alert('You got ' + answerCount + ' questions correctly! That means you get ' + rank);

1 Answer

Dane Parchment
MOD
Dane Parchment
Treehouse Moderator 11,077 Points

Here is your problem

if (answerCount = 5) { //The line here contains your error
  rank = 'gold crown!';
} else if (answerCount >= 3) {
  rank = 'silver crown!';
} else if (answerCount >= 1) {
  rank = 'bronze crown!';
} else {
  rank = 'no crown';
}

You must change it too

if (answerCount === 5) { //The line fixed
  rank = 'gold crown!';
} else if (answerCount >= 3) {
  rank = 'silver crown!';
} else if (answerCount >= 1) {
  rank = 'bronze crown!';
} else {
  rank = 'no crown';
}

Basically you were SETTING the variable answer count equal to the number five instead of actually CHECKING if it was equal five. In an if statement setting a variable equal to something will always return true.

ohtanya
ohtanya
7,768 Points

Gah! Thank you so much!