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

Why doesn't it work?

I've tried to complete the challenge and it didn't work so I watched the solution and pretty much copied it and i still doesn't work. I don't understand. https://w.trhou.se/tgj03a0dql Please help!

var quiz [ ["2x1?", 2], ["2x2?", 4], ["2x3?", 6], ];

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

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

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

html = "You got " + correctAnswer + " question(s) right."; print(html);

Could you paste the code here? Link to snapshot isn't working.

I added the code!

No problem, glad I could be of some help!

1 Answer

Seem like your issues were with the initial quiz var...

// you need to 'assign' the array to the quiz variable with an = sign
// also you have an extra comma after the last array element
// var quiz [["2x1?", 2], ["2x2?", 4], ["2x3?", 6], ]; 
var quiz = [
  ["2x1?", 2],
  ["2x2?", 4],
  ["2x3?", 6]
];

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

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

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

html = "You got " + correctAnswer + " question(s) right.";
print(html);

Of course!! How did I miss the = sign??? I actually noticed the extra comma right after i posted here but the most important I missed: the missing equal sign. Thank you so much for your help!