Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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