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

kevinkrato
kevinkrato
5,452 Points

Uncaught TypeError: Cannot set property 'innerHTML' of null

I've been trying to recreate this challenge from scratch. So far, so good, however im getting a console error. It claims that the error is starting somewhere at the bottom of the code (where the html is being written out).

I would post this in a nice screenshot but every time I add a comment (like this) asking how one does 'post a screenshot of the code' it gets overlooked.

anyway, blob follows.


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

var question; var answer; var userResponse; var userAnswer; var correctAnswer = 0; var wrongAnswer = 0; var correct = []; var incorrect = []; var html;

var quiz = [ ['How many states are in the United States?', 50], ['How many legs does an insect have?', 6], ['How many continents are there?', 7] ];

function buildList(arr) { var listHtml = '<ol>'; for (var i = 0; i < arr.length; i += 1) { listHtml += '<li>' + arr[i] + '</li>'; } listHtml += '</ol>;' return listHtml; }

for ( var i = 0; i < quiz.length; i += 1 ) { question = quiz[i][0]; answer = quiz[i][1]; userResponse = prompt( question ); userAnswer = parseInt( userResponse ); if ( userAnswer === answer ) { correctAnswer += 1; correct.push( question ); } else { wrongAnswer += 1; incorrect.push( question ); }
}

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

1 Answer

dragos busuioc
dragos busuioc
24,908 Points
var outputDiv = document.getElementById("output"); //the argument must be inside quotes