Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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!