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 JavaScript Loops, Arrays and Objects Tracking Multiple Items with Arrays Build a Quiz Challenge, Part 1 Solution

martin wong
martin wong
4,799 Points

document.write wont print

var counter = 0;
var conclusion;
var userGuess;
var answer;



var QuestionAndAnswer = [ 
["What is the capital of Great Britain?" , "London"],
["Which member left the boyband One Direction to pursue a solo career?" , "Zayn Malik"],
["What team won the Premier League in the 2016-17 season?" , "Chelsea"]
]


for (i = 0; i < QuestionAndAnswer.length; i+=1) { 
  QuestionAndAnswers  = QuestionAndAnswer [i] [0];
  answer = QuestionAndAnswer [i] [1];
  userGuess = prompt(QuestionAndAnswers);
  userguess = userGuess.toLowerCase();

  if (userGuess === answer) {
  correct +=1;
  }
}

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

html = "You got" + correct + "right!.";

print(html);

The code will not print how many answers i get right!. where am i going wrong? I have looked over the video code and mine for the print() and as far as i can see, i am doing the same thing

I am super demotivated by this course and feel like i cant go anywhere. Any other resources or something to help me out?

1 Answer

Steven Parker
Steven Parker
218,596 Points

This code increments a variable named "correct" when the given answer matches, and also uses "correct" to print the final result, but no variable by that name was ever established.

I'd guess you meant to put "var correct = 0;" up near the beginning of the program.

martin wong
martin wong
4,799 Points

Thank you very much!