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 Loops, Arrays and Objects Tracking Multiple Items with Arrays Build a Quiz Challenge, Part 1

Ben Ahlander
Ben Ahlander
7,528 Points

Uncaught SyntaxError: Unexpected identifier. The error is in my if statement.

function print(message) {
  document.write(message);
}

var questions = [
  ["Who is Bens favorite CEO?","ELON MUSK"],
  ["Why is 6 afraid of 7?","7 ate 9"],
  ["Is Ben smart?","YES"],
];

var rightAnswer = "";
var wrongAnswer = "";  

for (var i=0; i<questions.length; i+=1) {
  var answer = prompt[i][0];
  var questionNum = i + 1;

  if(answer[0].toUpperCase === questions[i][1]) {
    rightAnswer += "<h2>" questionNum +  questions[i][0] + "Your Answer = " + answer + ". Great Job! </h2>";
  } else {
    wrongAnswer += questionNum + questions[i][0] +
    "Your Answer = " + answer + "Correct Answer = " + questions[j][1];
  }
}

printAnswers("<h2><b>Questions Answered Correctly</b></h2>" + " rightAnswer );
printAnswers("<h2><b>Questions Answered Incorrectly</b></h2>" + wrongAnswer);

Moderator Edit: Added Markdown formatting so the code can be read in the Community. Please refer to the Markdown Cheatsheet before posting code in the Community for proper formatting, so that it is readable in the post.

2 Answers

Steven Parker
Steven Parker
229,771 Points

You have some missing or extra symbols.

When you combine strings, make sure there is a concatenation operator ("+").
There's one missing here :point_right: "<h2>" questionNum

And check for stray or unmatched quote marks.
There's an extra one here :point_right: + " rightAnswer );

And as a frequent poster, please learn how to quote your code using Markdown formatting to set a good example for others and save the moderators from needing to fix your questions!

Justin Roberts
Justin Roberts
14,583 Points

I think you need a '+' between '<h2>' and 'questionNum'