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

Liz Jackson
PLUS
Liz Jackson
Courses Plus Student 2,023 Points

Quiz score is not adding correctly - what am I doing wrong?

My quiz program runs and shows no errors in Chrome Developer Tools, but for some reason, the score variable is not adding correctly. Answering all questions incorrectly, all questions correctly, or (for some bizarre reason) four questions correctly will result in the correct end score, but answering between 1 and 3 questions correctly adds an extra point that shouldn't be there. I can't figure it out. Help!

// JavaScript Document

//*ask 5 questions and keep track of number of correct answers

var score = 0;

var question1 = prompt('Which Hindu deity is known as the god of destruction?');
if (question1.toUpperCase() === 'SHIVA') {
  score += 1;
}

var question2 = prompt('What is the capitol of North Dakota?');
if (question2.toUpperCase() === 'BISMARK') {
  score += 1;
}

var question3 = prompt('What animal has a long neck and brown spots?');
if (question3.toUpperCase() === 'GIRAFFE') {
  score += 1;
}

var question4 = prompt('How many continental states are there in the USA?');
if (question4.toUpperCase() === '48' || 'FORTY-EIGHT' || 'FORTY EIGHT') {
  score += 1;
}

var question5 = prompt('What is 35 * 2 - 4?');
if (question5 === '66') {
  score += 1;
}


//provide final answer after quiz with number of correct answers
//rank the player: 5/5 = gold, 3-4 = silver, 1-2 = bronze, 0 = no crown :(

if (score === 5) {
    alert('You answered ' + score + ' out of 5 questions correctly. Congratulations! You\'ve won a Gold Crown!');
} else if (score >= 3) {
    alert('You answered ' + score + ' out of 5 questions correctly. Congratulations! You\'ve won a Silver Crown!');
} else if (score >+ 1) {
    alert('You answered ' + score + ' out of 5 questions correctly. Congratulations! You\'ve won a Bronze Crown!');
} else {
    alert('Bummer! You didn\'t answer any questions correctly. Try again!');
}
Liz Jackson
Liz Jackson
Courses Plus Student 2,023 Points

smh... I just figured it out... I wrote (score >+ 1) instead of (score >= 1) for one of my conditional statements. The devil really is in the details! Thanks anyways guys :)

1 Answer

Hi Liz, I added the below to question 4 and it worked.

var question4 = prompt('How many continental states are there in the USA?');
if (question4.toUpperCase() === '48' || question4.toUpperCase() ===  'FORTY-EIGHT' || question4.toUpperCase() === 'FORTY EIGHT') {
  score += 1;