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

nicole grima
nicole grima
5,028 Points

Can't make my solution work. So close!

Really want my code to work for this challenge - I came really close on my own... but got stuck. I incorporated all the changes in the solution videos - but still no luck. Any suggestions?

var questionsAnswers = [
  ['What is my name?', 'Name'], 
  ['What is my middle name?', 'Middle'], 
  ['What is my surname?', 'Surname']
];

var correct = 0; 
var question; 
var answer; 
var response; 
var html; 
var correctAnswers = []; // added array []
var wrongAnswers = []; // added array []

function print(message) { // incorporated new method 
  var outputDiv = document.getElementByID('output'); 
  outputDiv.innerHTML = message; 
} 

function buildList(arr) { // to build the list of correct and wrong Qs 
  var listHTML = '<ol>'; // arr is an array as a parameter 

  for (var i = 0; i < arr.length; i += 1) {
  listHTML += '<li>' + arr[i] + '</li>';
  }

  listHTML += '</ol>';
  return listHTML;
}


for (var i = 0; i < questionsAnswers.length; i += 1) { // I was missing this loop
  question = questionsAnswers[i][0];
  answer = questionsAnswers[i][1];
  response = prompt(questionsAnswers);

  if (response === answer) { // Man I was so close!! 
      correct += 1; 
      correctAnswers.push(question); 
  } else {
      wrongAnswers.push(question); 
  }
}

html = You got " + correct +  right.; 

html += '<h2>You go these questions correct: </h2>'; 
html += buildList(correctAnswers);
html += '<h2>You go these questions wrong: </h2>'; 
html += buildList(wrongAnswers);
print(html);

I wonder what might happen if you declared listHTML in the global scope (with the other variable declarations near the top). That seemed to make a difference for me.

Nevermind - the variable declaration for listHTML works for me in either location. I took a look at your code in my browser. The console flagged two errors. When I fixed them it ran.

  1. After the for loop, the first html variable addition/assignment statement uses double quotes. When I changed it to single quotes I no longer received the error message. I think this might be because this is JavaScript concatenation that will be inserted as html. Single quotes are standard for JS, double quotes for html.
  2. In the print function, there's a typo: .getElementByID needs a lower case 'd' at the end.

The homie Roslynn is correct about the single quotes and the getElementById function, and I would add that in the "response" prompt, the parameter is set to " questionsAnswers ", which makes it run the entirety of the array in the first prompt. If you switch it to " questions " it runs perfectly!

Toshi Sosa
Toshi Sosa
8,004 Points

It worked after I made three changes

  1. from getElementByID to getElementById
  2. from response = prompt(questionsAnswers); to response = prompt(question);
  3. re-typed "" on the line html = "You got " + correct + " right."; (I did not use single quotes)

This may be recap of earlier post, but I hope this will help. Good luck.