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

Uncaught SyntaxError: Unexpected number...?

Can anyone tell me why this would throw a syntax error?

var questions = [
  ['Which fictional city is the home of Batman?', 'Gotham City'],
  ['In which sport would you perform the Fosbury Flop?', 'The high jump.'],
  ['Spinach is high in which mineral?', 'Iron'],
  ['What is a Geiger Counter used to detect?', 'Radiation']
]

var correctAnswer = 0;
var question;
var answer;
var response;
var html;

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

for ( var i = 0, 1 < questions.length; i =+ 1 ) {
  question = questions[i][0];
  answer = questions[i][1];
  response = str.toLowerCase(prompt(question));
  if ( response === answer) {
    correctAnswer += 1;
  }
}

html = "You got " + correctAnswer + "questions right!";
print(html);

EDIT: The fix

var questions = [
  ['Which fictional city is the home of Batman?', 'Gotham City'],
  ['In which sport would you perform the Fosbury Flop?', 'High jump'],
  ['Spinach is high in which mineral?', 'Iron'],
  ['What is a Geiger Counter used to detect?', 'Radiation']
]

var correctAnswer = 0;
var question;
var answer;
var response;
var html;

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

for ( var i = 0; i < questions.length; i += 1 ) {
  question = questions[i][0];
  answer = questions[i][1];
  response = prompt(question);
  if ( response === answer) {
    correctAnswer += 1;
  }
}

html = "You got " + correctAnswer + " questions right!";
print(html);

I'll have to be more careful with typing out my for loops. Thanks everyone :)

2 Answers

for ( var i = 0, 1 < questions.length; i =+ 1 ) {

Should be changed to use a semi-colon:

for ( var i = 0; 1 < questions.length; i =+ 1 ) {

There's some other errors in the code, but hopefully that helps you get on the right track.

Thank you for the heads up. You are right, I corrected that and some other lines, now it won't proceed past the second question...

edit: Got it, see edited post for fix! Thanks again!

Hi Ryan,

I'm certainly not the best programmer, yet, but wouldn't your conditional statement in the for loop need to be set to i instead of 1? I may be way off like i said :)

Take Care!

Thank you, good catch. I knew it was going to be something small.