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

Ben Schroeder
Ben Schroeder
22,818 Points

Why does the user's score on this quiz always end up at 0?

Regardless of whether or not I put in the correct answer, I always end up with a score of 0 and no crown.

var score = 0;

var answer1 = prompt("What is 1 + 1?");
var answer2 = prompt("What is 3 + 6?");
var answer3 = prompt("What is 2 + 2?");
var answer4 = prompt("What is 10 + 10?");
var answer5 = prompt("What is 5 + 9?");

if(answer1 === parseInt(2)){
  score += 1;
}
if(answer2 === parseInt(9)){
  score += 1;
}
if(answer3 === parseInt(4)){
  score += 1;
}
if(answer4 === parseInt(20)){
  score += 1;
}
if(answer5 === parseInt(14)){
  score += 1;
}

document.write("<p>Out of 5 questions, you got <strong>" + score + "</strong> correct.</p>")

if(score === 5){
  document.write("<p>Congratulations! You get the gold crown.</p>");
} else if (score === 4 || score === 3){
  document.write("<p>Nice. You get the silver crown.</p>");
} else if (score === 2 || score === 1){
  document.write("<p>Okay. You got the bronze crown.</p>");
} else {
  document.write("<p>No crown for you!</p>");
}

2 Answers

Ben Schroeder
Ben Schroeder
22,818 Points

And, of course, I managed to figure it out as soon as I posted this question! That's how it works, I suppose.

This is the code I should have been using to determine the score:

if(parseInt(answer1) === 2){
  score += 1;
}
if(parseInt(answer2) === 9){
  score += 1;
}
if(parseInt(answer3) === 4){
  score += 1;
}
if(parseInt(answer4) === 20){
  score += 1;
}
if(parseInt(answer5) === 14){
  score += 1;
}

Yeah if you ever get stuck on something always rubber duck it before doing anything else... https://en.wikipedia.org/wiki/Rubber_duck_debugging