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

J V
J V
3,414 Points

Build a Quiz Part 2: Unsure as to why my code does not print-out properly

var questionsArray = [

  ["What is the capital of Georgia?", "Atlanta"],
  ["What is the capital of Alabama?", "Montgomery"],
  ["What is the capital of Tennessee?", "Nashville"],
  ["What is the capital of Florida?", "Tallahassee"],
  ["What is the port city in Georgia?", "Savannah"]

  ];

var questionsCorrect = [];
var questionsIncorrect = [];

var question;
var answer;
var user_response; 

var correctAnswers = 0;
var incorrectAnswers = 0;

function work(arrayhold)
{
   for(i=0; i < arrayhold.length; i++)
  {
    question = arrayhold[i][0];

     answer = arrayhold[i][1];

    user_response = prompt(question);

    if(user_response.toLowerCase() === answer.toLowerCase())
    {
      correctAnswers = correctAnswers + 1;
       questionsCorrect.push(question);
    }
    else
    {
         incorrectAnswers = incorrectAnswers + 1;
         questionsIncorrect.push(question);
    }

  }
}


function buildList(array)
{
  var HTML = "<ol>";

   for(i=0; i < array.length; i++)
  {
    HTML = HTML + "<li>" + array[i] + "</li>";
  }
  HTML = "</ol>";
  return HTML;
}


work(questionsArray);

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


HTML = "You got these answers correct: " + correctAnswers;
HTML += "<h2>You got these question correct: </h2>";
HTML += buildList(questionsCorrect);

HTML += "<h2>You got these questions incorrect: </h2>";
HTML += buildList(questionsIncorrect);

print(HTML);

2 Answers

LaVaughn Haynes
LaVaughn Haynes
12,397 Points
function buildList(array)
{
  var HTML = "<ol>";

   for(i=0; i < array.length; i++)
  {
    HTML = HTML + "<li>" + array[i] + "</li>";
  }

  //You override HTML's value
  //HTML = "</ol>";
  //Make it: HTML += theString; or HTML = HTML + theString;
  HTML += "</ol>";
  return HTML;
}
J V
J V
3,414 Points

Hi LaVaughn Haynes,

Thank you for your input. I overlooked such a small mistake.