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 1

Luqman Shah
Luqman Shah
3,016 Points

Everything works, except document.write

So I managed to execute the pain part of the challenge, I just can't get it to print onto the document for some reason, please review my code. For an in-depth review: (https://w.trhou.se/20h2whjj0e)

function print(message) {
  document.write(message);
}

var questionS = [
  ['How many states does North America have?', '50'],
  ['Does a lion walk on 2 or 4 legs?', '4'],
  ['Is California located in the East Coast, or the West Coast of North America?','West Coast']
];

var correctAnswers = 0;
var wrongAnswers = 0;

function printQuestions(questions) {
  var listHTML = '<ol>';
  for ( var i = 0; i <= questionS.length; i += 1 ) {
    var question = prompt(questionS[i][0]);
    if ( question === questionS[i][1] ) {
      correctAnswers += 1;
      listHTML += '<li>' + questionS[i][1] + '</li>';
    } else {
      wrongAnswers += 1;
    }
  }
  listHTML += '</ol>';
}

printQuestions(questionS);

document.write('You got ' + correctAnswers + ' correct!');
document.write('You got ' + wrongAnswers + ' incorrect.');

1 Answer

Steven Parker
Steven Parker
229,644 Points

In your loop conditional clause you test if the index is less than or equal to the length ("i <= questionS.length"). But the highest index number should be less than the length ("i < questionS.length"). This causes an error which stops the program before it reaches the document.write statements.