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

Rod R
Rod R
3,050 Points

html print not working

Why wont my print(html) work? the statement never appears on my browser screen(chrome):

prompt("Ready to take a three question quiz about the USA? Click OK to proceed.");

// two dimensional array with questions and answers var questions = [ ['How many states are in the United States?', 50], ['How many legs does an octupus have?', 8], ['How many countries are in North America?', 23] ];

var gotRight = 0; var gotWrong = 0; var question; var answer; var response; var html;

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

for (var i = 0; i <= questions.length; i++) {
  question = questions[i][0]; 
  answer = questions[i][1]; 
  response = parseInt(prompt(question));
    if (response === answer) {
      gotRight++; 
    alert("That is correct!"); 
    } else 
      alert("Thats not correct"); 

}

//print how many questions were correctly answered html = "You got " + gotRight + " question(s) right."; print(html);

2 Answers

This:

for (var i = 0; i <= questions.length; i++) {

should be this:

for (var i = 0; i < questions.length; i++) {

for the loop to exit properly.

Rod R
Rod R
3,050 Points

thank you for the information and fast response!