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 Solution

Ayush Bahuguna
Ayush Bahuguna
2,092 Points

Please help me with my solution.

I haven't checked instructor's solution. I want to know what is wrong with mine. It is working great except that it displays results one by one. I mean all right answers are not displayed under one heading.

Please take a look.

var quiz = [
  ['What is the capital of India?','NEW DELHI'],
  ['What is the capital of Japan?' ,'TOKYO'],
  ['What is the capital of Pakistan?','ISLAMABAD'],
  ['What is the capital of USA?','WASHINGTON DC']
];

var rightCount = '';

var falseCount = '';

var answer;

for( var i=0; i<quiz.length ; i ++){
  answer = prompt(quiz[i][0]);
  if(answer.toUpperCase() === quiz[i][1]){
    rightCount = document.write('You got these questions right' + '<br>' + '<ol><li>' + quiz[i][0] + '</li></ol>');

  }else{
    falseCount = document.write('You got these questions wrong' + '<br>' + '<ol><li>' + quiz[i][0] + '</li></ol>'); 
  }
}```

1 Answer

Grace Kelly
Grace Kelly
33,990 Points

Hi Ayush, the main issue is you have your "you got these questions correct/wrong" inside the for loop, which means they are printed out during each iteration of the loop:

for( var i=0; i<quiz.length ; i ++){
  answer = prompt(quiz[i][0]);
  if(answer.toUpperCase() === quiz[i][1]){
    rightCount = document.write('You got these questions right' + '<br>' + '<ol><li>' + quiz[i][0] + '</li></ol>');

  }else{
    falseCount = document.write('You got these questions wrong' + '<br>' + '<ol><li>' + quiz[i][0] + '</li></ol>'); 
  }
}

This is why you are not getting the answers displayed under one heading. I edited your code slightly (i hope you don't mind) that instead of writing out the result, it insteads appends it to a variable and then prints it after the loop:

var quiz = [
  ['What is the capital of India?','NEW DELHI'],
  ['What is the capital of Japan?' ,'TOKYO'],
  ['What is the capital of Pakistan?','ISLAMABAD'],
  ['What is the capital of USA?','WASHINGTON DC']
];

 //assign headings outside of for loop
var rightCount = 'You got these questions right' + '<br>';

var falseCount = 'You got these questions wrong' + '<br>';

var answer;

var html = "";  //create an empty html variable to store quiz data later

for( var i=0; i<quiz.length ; i ++){
  answer = prompt(quiz[i][0]);
  if(answer.toUpperCase() === quiz[i][1]){
    rightCount += '<ol><li>' + quiz[i][0] + '</li></ol>'; //append answer to rightCount with "+="

  }else{
    falseCount += '<ol><li>' + quiz[i][0] + '</li></ol>'; //append answer to falseCount with "+="
  }
}

html += rightCount + falseCount //add the rightCount and falseCount variables to html

document.write(html) //print out html

Hope that helps!!