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

Darren Healy
seal-mask
.a{fill-rule:evenodd;}techdegree
Darren Healy
Front End Web Development Techdegree Student 23,565 Points

Arrgh, stuck on completing challenge

Hi folks,

Looking for some help with this challenge. I can't get the qCorrect variable to update, and the questions which are right/wrong do not print to the document. I'm just looking at my code and struggling to find the issue.

var qCorrect = 0;
var question;
var answer;
var response;
var html;
var qAndA = [ ["Name a continent beginning with E", "europe" ],
            [ "What orbits the Earth?", "moon" ],
            [ "What is the first month of the year?", "january" ] ];
var correct = [];
var wrong = [];


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


function questionList(list) {
  var listHTML = '<ol>';
  for (var i = 0; i < list.length; i += 1) {
    listHTML += "<li>" + list[i] + "</li>";
  }
  listHTML += '</ol>';
  return listHTML;
}


function askQuestions(questions) {
  for (var i = 0; i < questions.length; i += 1) {
  question = prompt(questions[i][0]);
    if (question === questions[i][1]) {
      qCorrect += 1;
      correct.push(questions[i][0]);
    } else {
      wrong.push(questions[i][0]);
    }
  }
}

html = "<p>You got " + qCorrect + " question(s) right.</p>";
html += "<h2>You got these questions correct:</h2>";
html += questionList(correct);
html += "<h2>You got these questions wrong:</h2>";
html += questionList(wrong);

askQuestions(qAndA);
print(html);

console.log(correct.length);
console.log(correct);
console.log(wrong.length);
console.log(wrong);

Dave McFarland?

1 Answer

The order of your code is causing the issue. Your code should be re-arranged as:

  • call the 'askQuestions(qAndA)' function
  • build your html
  • 'print(html)'

Here is the re-ordered code:

var qCorrect = 0;
var question;
var answer;
var response;
var html;
var qAndA = [ ["Name a continent beginning with E", "europe" ],
            [ "What orbits the Earth?", "moon" ],
            [ "What is the first month of the year?", "january" ] ];
var correct = [];
var wrong = [];


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


function questionList(list) {
  var listHTML = '<ol>';
  for (var i = 0; i < list.length; i += 1) {
    listHTML += "<li>" + list[i] + "</li>";
  }
  listHTML += '</ol>';
  return listHTML;
}


function askQuestions(questions) {
  for (var i = 0; i < questions.length; i += 1) {
  question = prompt(questions[i][0]);
    if (question === questions[i][1]) {
      qCorrect++;
      correct.push(questions[i][0]);
    } else {
      wrong.push(questions[i][0]);
    }
  }
}

/* This line of code should come first before populating your html output below*/
askQuestions(qAndA);

html = "<p>You got " + qCorrect + " question(s) right.</p>";
html += "<h2>You got these questions correct:</h2>";
html += questionList(correct);
html += "<h2>You got these questions wrong:</h2>";
html += questionList(wrong);

/* After you have populated your html, you can now print to screen*/
print(html);

console.log(correct.length);
console.log(correct);
console.log(wrong.length);
console.log(wrong);

Cheers!

You are welcome Darren :)