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

The questions gotten right or wrong isn't displaying

Is there a problem with my code?

var numb; var question; var response; var gotAnswers; var answer; var answeredCorrectly = 0; var correct = []; var wrong = [];

function print(message){ var outputDiv = document.getElementById('output'); outputDiv.innerHTML = message;
}

function info(arra){ var numb = '<ol>'; for(var i; i < arra.length; i ++){ numb += '<li>' + arra[i] + '</li>'; } numb += '</ol>'; return numb; }

var test = [ ['What is force per unit area?', 'pressure'], ['How many states are in Nigeria?', '36'], ['What is the universal solvent?', 'water'], ['How many abs does a human being have?', '6'], ['What is the most expensive apple smartphone?', 'iphone x'] ];

for(var c = 0; c < test.length; c ++){ question = test[c][0]; answer = test[c][1]; response = prompt(test[c][0]); if(response === answer){ answeredCorrectly ++; correct.push(question); }else{ wrong.push(question); } }

gotAnswers = 'You answered ' + answeredCorrectly + ' question(s) correctly'; gotAnswers += '<h2>' + 'You got these questions right:' + '</h2>'; gotAnswers += info(correct); gotAnswers += '<h2>' + 'You got these questions worong:' + '</h2>'; gotAnswers += info(wrong); print(gotAnswers);

2 Answers

Steven Parker
Steven Parker
220,937 Points

The loop in the info function doesn't initialize the index variable:

  for (var i; i < arra.length; i++) {      // <-- current code
  for (var i = 0; i < arra.length; i++) {  // <-- with fix added

In future questions, use the instructions for code formatting in the Markdown Cheatsheet pop-up below the "Add an Answer" area. :arrow_heading_down:   Or watch this video on code formatting.

Thank you very much Steven, my program is up and running now.