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

Shreyas Sreenivasa
Shreyas Sreenivasa
7,804 Points

Undefined appears instead of the question.

Instead of displaying the question is prints out undefined.

My code:

var questonsAndAnser = [['What is 1 + 1?', 2], 
                        ['How many states are there in India?', 29], 
                        ['How many planets are there in Solar System', 8]];
var question;
var correctAnswers = 0;
var answer;
var response;
var html = "";
var correct = [];
var wrong  = [];
var correctOl = document.getElementById('correct');
var wrongOl = document.getElementById('wrong');

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

function buildList(arr){
  var listHtml = '<ol>';
  for (var i = 0; i < arr.length; i += 1) {
    listHtml += '<li>' + arr[i] + '</li>';
  listHtml += '</ol>';

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

  if (response === answer) {
    correctAnswers += 1;
  } else {

html = '<h3> You got these questions correct: </h3>';
html += buildList(correct);
html += '<h3> You  got these questions wrong </h3>';
html += buildList(wrong);


2 Answers

Jon Kealy
Jon Kealy
9,082 Points

You didn't use "return listHTML;" at the end of the buildList function.

Rohit Tolawat
Rohit Tolawat
8,277 Points

Hi Shreyas,

Your code looks correct to me.... One thing you might be missing is the semi-colon after the last statement... Also... the variable correctol and wrongol are not used... check that as well..

Let me know if it works out.

Thanks, Rohit.