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

Challenge

Can anyone tell me why I have to answer all three questions before it will write whether I answered that question correctly or incorrectly to the document?

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

var questionAnswer = [['How old am I?', '26'],['What am I listening to?', 'Bligg'],['Where am I going tomorrow?', 'Central London']];

var questionsAsked = 0; var correctAnswers = 0;

for (var i=0; i<3; i +=1) { var answer = prompt(questionAnswer[i][0]); questionsAsked +=1; if (answer === questionAnswer [i] [1]) { correctAnswers +=1; print("Question number" + (i+1) + "answered correctly"); } else { print("Boo, Question number" + (i+1) + "answered incorrectly"); } } print (correctAnswers + "of" + questionsAsked + "questions were answered correctly");'''

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

var questionAnswer = [['How old am I?', '26'],['What am I listening to?', 'Bligg'],['Where am I going tomorrow?', 'Central London']];

var questionsAsked = 0;
var correctAnswers = 0;

for (var i=0; i<3; i +=1) {
  var answer = prompt(questionAnswer[i][0]);
  questionsAsked +=1;
  if (answer === questionAnswer [i] [1]) {
    correctAnswers +=1;
    print("Question number" + (i+1) + "answered correctly");
  }
   else {
    print("Boo, Question number" + (i+1) + "answered incorrectly");
   }
  }
print (correctAnswers + "of" + questionsAsked + "questions were answered correctly");
```javascript

1 Answer

When I run your code it is writing as entered...I think it may appear that all three are being answered before getting a response because it is just writing it to the webpage and bringing up another dialogue box really quickly.