Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript JavaScript Loops, Arrays and Objects Tracking Multiple Items with Arrays Build a Quiz Challenge, Part 1 Solution

Julianna Kahn
Julianna Kahn
20,700 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,700 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,700 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,700 Points

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