Bummer! You must be logged in to access this page.

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

Code? Why won't my JavaScript counter work?

prompt("Please answer five questions.");

var year = window.prompt("What year was the first Magnum P.I. aired?"); var island = window.prompt("What island was the show filmed on?"); var dogs = window.prompt("What does Higgens call his dogs?"); var friend = window.prompt("What is the first name of Magnum's naval friend?"); var yearsRan = window.prompt("How many years did the show run?");

var score = 0;

if (year === 1980) { score =+ 1; } else if (island.toUpperCase() === "OAHU") { score += 1; } else if (dogs.toUpperCase() === "LADS") { score += 1; } else if (friend.toUpperCase() === "MAC") { score += 1; } else if (yearsRan === 8) { score += 1; } else { score += 1; }

alert(You got" + score + "answers correct");

if (score === 5) { alert("Awesome!"); }

else if (score >= 3) { alert("Pretty Good!"); } else { alert("We better watch more Magnum!"); }

1 Answer

Hi Rachel,

There were some mistakes in your code, but no worries! There was a parse error in your code and also some logic errors. You don't want to use else ifs unless you're using the same variable to compare to something else in each statement. Since you're using different variables, you need to leave the else off of the if statement.

What happens, in your chain of else if statements, is that if the first answer isn't true, then the next if statement will execute (and so on and so forth) but if you guessed the right year, the next if statements in the chain of else-if statements won't execute and you only get 1 added to score.

By changing the else if statements to just if statements, each individual if statement will execute, which is what you want to happen.

The parse error came from a missing " at the beginning of your first alert.

//changed prompt to alert because there's no input gathered here
alert("Please answer five questions.");
//the window object isn't needed here because every default command is
//a part of the window object
var year = prompt("What year was the first Magnum P.I. aired?"); 
var island = prompt("What island was the show filmed on?"); 
var dogs = prompt("What does Higgens call his dogs?"); 
var friend = prompt("What is the first name of Magnum's naval friend?"); 
var yearsRan = prompt("How many years did the show run?");

var score = 0;

//changed 1980 to "1980" because prompts always return strings
//and so the comparison needs to happen against a string not a number value
//also changed =+ to +=
if (year === "1980") { 
score += 1; 
} 
if (island.toUpperCase() === "OAHU") { 
score += 1; 
}
if (dogs.toUpperCase() === "LADS") { 
score += 1; 
}
if (friend.toUpperCase() === "MAC") { 
score += 1; 
} 
//changed to 8 to "8" because of same reason as above
if (yearsRan === "8") { 
score += 1; 
}

//missing " at beginning of string
//also added spacing
alert("You got " + score + " answers correct");

if (score === 5) { 
alert("Awesome!"); 
}
else if (score >= 3) { 
alert("Pretty Good!"); 
} 
else { 
alert("We better watch more Magnum!"); 
}

Wow, thanks for the quick and thorough reply. It works :)

You're welcome, Rachel! It's what I do haha Happy Coding :)