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

Brian Stumbaugh
Brian Stumbaugh
2,161 Points

What is wrong with my code?

when I answer all 5 correctly, I still only get the silver crown.

var correct = 0;

var answer1 = prompt("what is your name?");
if (answer1.toUpperCase() === 'CHRIS') {
  correct += 1;
}

var answer2 = prompt("what is your age?");
if (answer2 === 36) {
  correct += 1;
}

var answer3 = prompt("what is your species?");
if (answer3.toUpperCase() === "HUMAN") {
  correct += 1;
}

var answer4 = prompt("what sex are you?");
if (answer4.toUpperCase() === 'MALE') {
  correct += 1;
}

var answer5 = prompt("are you older than 35?")
if (answer5.toUpperCase() === 'YES') {
  correct += 1;
}


if (correct === 5) {
  alert('you win the gold crown!');
} else if (correct >= 3) {
    alert('you win the silver crown');
} else if (correct === 2) {
    alert('you win the bronze');
} else {
    alert("you are a dummy.");
}

4 Answers

Hey Chris,

Two Errors:

  • You're missing a semicolon after line 23.
  • On the answer two variable you are adding one if the user enters the NUMBER 36, but you need to convert the variable to a number as it is a string value. Or just change that line to:
if (answer2 === '36') {
  correct += 1;
}
Brian Stumbaugh
Brian Stumbaugh
2,161 Points

thank you! I made the changes...and just did what you posted above for answer2.

If I did want to do it the other way like you suggested.....would I use parseInt? would it be like:

if (answer2 === parseInt(36)) {

or am I just totally wrong doing that?

Yes you could absolutely do that, probably better that way as you could then take that and do equations with it if you wanted to but the code would be:

if (parseInt(answer2) === 36){ 
correct += 1;
}

You need to change the variable "answer2" to a number

No prob! Mark my answer as best answer if you don't mind ;)

thanks man ;-)