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 Solution

Julianna Kahn
Julianna Kahn
20,702 Points

I keep getting errors on the lines requesting data from the array. It seems not to recognize the i variable?

var questions = [ ['What is the biggest city in Illinois?', 'chicago'], ['What color do you get when you mix red and blue?', 'purple'], ['What music group sang Eleanor Rigby?', 'beatles'] ];

function print(message) { document.write(message); console.log( '<p>' + message + '</p>'); }

var correctAnswers = 0; var quest; var answer; var response;

for (var i = 0; i < questions.length; i += 1); { quest = questions[i][0]; answer = questions[i][1]; response = parseInt(prompt(quest)); if(response === answer){ print('You answered correctly'); correctAnswers +=1; } }

print('You got ' + correctAnswers + ' answers right');

7 Answers

You have a semi-colon on line 17 or so, right after you start the for loop. The loop will run its course once its removed.

As a suggestion use Markdown when posting your code as it will help the community read your code. The semi-colon was removed from the code below.

  var questions = [
    ['What is the biggest city in Illinois?', 'chicago'],
    ['What color do you get when you mix red and blue?', 'purple'],
    ['What music group sang Eleanor Rigby?', 'beatles']
  ];

  function print(message) {
    document.write(message);
    console.log('<p>' + message + '</p>');
  }

  var correctAnswers = 0;
  var quest;
  var answer;
  var response;

  for (var i = 0; i < questions.length; i += 1) { 
    quest = questions[i][0];
    answer = questions[i][1];
    response = parseInt(prompt(quest));
    if (response === answer) {
      print('You answered correctly');
      correctAnswers += 1;
    }
  }

  print('You got ' + correctAnswers + ' answers right');

You also don't need the parseInt function since your answers are text (instead of numbers as in the video). You should however convert the response to lowercase in your comparison since your answers are lowercase.

Julianna Kahn
Julianna Kahn
20,702 Points

So it was the semi-colon, thank you. I must have been looking at that if statement for an hour. And thanks for bringing the lowercase statement to my attention. That was the fix I had intended but was probably too exhausted from not getting the rest of it to work. So thanks again.

But I'm not sure what you mean by using markdown. Is that the workbook snapshot feature. I was not sure how to copy the http it gave me.

Carl Evison
Carl Evison
2,656 Points

If you click on the Markdown Cheatsheet below the text area you'll see a section on adding code to get syntax highlighting, there you can replace the word html with the language you're using, it'll make it easier for people to spot your mistakes and get your question answered quicker.

Julianna Kahn
Julianna Kahn
20,702 Points

I still don't understand. First, it looks like this is only for HTML. Also, I saw several markings. Does that mean there is a type of marking you are suppose to put in front of each line?

Carl Evison
Carl Evison
2,656 Points

replace html with JavaScript after the first 3 ``` and then your code snippet below the ```JavaScript line, that will produce

document.write('Hello');
Julianna Kahn
Julianna Kahn
20,702 Points

I think I've got it, Enter this one line before all the rest of the code is pasted.