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

Where I am doing wrong in this quiz app? When I am running it, I get different answers. Pls guide me.

var correct = 0; var questionNumber = 5; var questionInfo = '[You have '; var userQuestionOne = prompt('Who Played Rocky in movie Rocky? ' + questionInfo + questionNumber + ' questions left]'); if (userQuestionOne.toLowerCase() === 'sylvester stallone') { correct = 1; } questionNumber -= 1; var userQuestionTwo = prompt('What is Spiderman\'s real name? ' + questionInfo + questionNumber + ' questions left]'); if (userQuestionTwo.toLowerCase() === 'peter parker') { correct = 2; } questionNumber -= 1; var userQuestionThree = prompt('Who Directs Avatar movie? ' + questionInfo + questionNumber + ' questions left]'); if (userQuestionThree.toLowerCase() === 'james cameron') { correct = 3; } if (correct >= 2 ) { alert('You got a gold crown.'); } else if (correct == 1) { alert('You got a silver crown.'); } else { alert('You score zero. Try again'); }

2 Answers

Jason Cook
Jason Cook
11,403 Points

Abhijeet,

It looks like just a simple mistake is keeping this code from working correctly. I believe you are intending to increment the "correct" variable by 1, each time they answer a question correctly. However, your code is overwriting the value, instead.

Here's the fix (just change the three if statements as follows):

if (userQuestionOne.toLowerCase() === 'sylvester stallone') {
  correct += 1;
}
if (userQuestionTwo.toLowerCase() === 'peter parker') {
  correct += 1;
}
if (userQuestionThree.toLowerCase() === 'james cameron') {
  correct += 1;
}

This causes the "correct" variable to be incremented by 1, with each new correct answer, which will solve your issue.

I hope this helps! Happy coding :-)

Thank you Jason. It's clear to me now :). Really helpful.

Instead writing in the code 'correct = 1' or 'correct = 2'. Try correct = correct + 1; correct += 1; or correct ++;

Dear Tommy, Thank you for your help. Now I can understand the mistake. One question i have in my mind, that is using the correct ++ as an increment operator (++) in console, I am getting confusable outcomes like : var correct = 0; correct ++; 0 //why I am getting 0 here? should not be the var store numeric 1, instead of 0?// correct ++; 1 //here should be the var store numeric 2, instead of 1?// please help me to clear this. Thanks.

I think I made a mistake i put a space between correct and ++. So instead of correct ++, its supposed to be correct++. I hope that solves your problem.