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 2 Solution

Been staring at this code for an hour!

Cannot figure out what the problem is. In the console it says there is an uncaught type error on the line that establishes the question variable. When I click on the error is says (anonymous function) but from what I can see there are no anonymous functions, questions is correctly defined, and I can't see any other mistakes!!!! PLEASE HELP!

var QandA = [
['How many fingers do you have?', '10'],
['who is your little bro?', 'dan'],
['where are you?', 'Sweden']

var right = [];
var wrong = [];

var question = QandA[i][0];
var answer = QandA[i][1];
var response = prompt(question);
var html;

function listGenerator (arr) {
    var listHTML = "<ol>";
    for (i = 0; i < arr.length; i++) {
        listHTML += '<li>' + arr[i] + '</li>';
    listHTML += '</ol>';
    return listHTML;

for (var i = 0; i < QandA.length; i++) {
    if (response === answer) {
    } else {
html = document.write('You got these questions right: ');
html += document.write(listGenerator(correct));
html += document.write('You got these questions wrong');
html += document.write(listGenerator(wrong));

2 Answers

Yazeed Bzadough
Yazeed Bzadough
4,880 Points

I agree with Stephen O'Connor, I don't think it is legal to have var question = QandA[i][0]; because the variable i has not been defined yet. It also seems like you called listGenerator() on the variable correct, but that's not anywhere in your script. I think you meant to call right, as that's the array you intended to hold correct answers in.

Also, in my opinion, a better way to set the QandA variable up is to make a bunch of QuizQuestion objects inside an array. Each object could have a question, and an answer. It may look something like this:

    question: 'How many fingers do you have?',
    answer: 10

That way, you can just reference QandA[0].question and QandA[0].answer

You dogg Yazeed!!!!! Thank you so much! WOW! Great lesson to learn about i!!!

Stephen O'Connor
Stephen O'Connor
22,291 Points

Should you not have var i = 0 in the for loop in your function?