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

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;
    correct.push(question);
  } else {
    wrong.push(question);
  }
}

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

print(html)

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.