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 trials speir
3,991 PointsWhy am I receiving 'Unexpected token +=' in the console?
Hello!
I came to my version of a solution for this challenge that worked.
I re-wrote my version to match the example answer created by Dave, but with my questions.
However, I cannot get it to work because the console reports that there is an 'Unexpected token +=' on every line that uses :
var correct += 1;
So, what am I missing? Dave uses that exact syntax as the answer, so what's wrong with my use of it?
Thank you!
Here is my full code below :
/*
Challenge Instructions
Ask at least five questions
Keep track of the number of questions the user answered correctly
Provide a final message after the quiz letting the user know the number of questions he or she got right.
Rank the player. If the player answered all five correctly, give that player the gold crown: 3-4 is a silver crown; 1-2 correct answers is a bronze crown and 0 correct is no crown at all.
*/
var correct = 0;
var answer1 = prompt ('What is your name?');
if (answer1.toUpperCase() === 'SPEIR') {
var correct += 1;
}
var answer2 = prompt ('What is 2+2?');
if (answer2.toUpperCase() === 4 ) {
var correct += 1;
}
var answer3 = prompt ('What is' + answer2 + ' + 2?');
if (answer3.toUpperCase() === 6 ) {
var correct += 1;
}
var answer4 = prompt ('What is' + answer3 + ' + 2?');
if (answer4.toUpperCase() === 8 ) {
var correct += 1;
}
var answer5 = prompt ('What is' + answer4 + ' + 2?');
if (answer5.toUpperCase() === 10 ) {
var correct += 1;
}
/*
OUTPUT RESULTS
*/
document.write ("<h1>Looks like you got " + correct + " out of 5 questions correct!");
/*
OUTPUT RANK
*/
if ( correct === 5) {
document.write ("<h1><strong>You recieved a Gold Crown!<strong></h1>");
}
else if ( correct >= 3) {
document.write ("<h1><strong>You recieved a Silver Crown!<strong></h1>");
}
else if ( correct >= 1) {
document.write ("<h1><strong>You recieved a Bronze Crown!<strong></h1>");
}
else ( correct >= 3) {
document.write ("<h1><strong>No crown for you ... sorry!<strong></h1>");
}
2 Answers
Marie Nipper
22,638 PointsYou are re-declaring, or trying to re-declare the variable "correct" in each answer block. Declare it once at the top like you did and then mutate it within the answer block. So remove the keyword var from each answer block and then try to run it again.
Alan Olvera
1,928 PointsThe problem is when you are declaring many "correct" variables, you just need one at the top, then you should start to assign different values to it.
s speir
3,991 Pointss speir
3,991 PointsAh!
That makes sense. Thank you, it works now.