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 2 Solution

Konrad Dziekonski
Konrad Dziekonski
7,798 Points

code review

Hello,

I managed to finish the task, but I did not write a function to print the list I just used <br> tag, I know its more primitive ;] But my question is that I did not use the push method to add correct or incorrect answers to those extra arrays I have created, only +=. What implications it has? I am asking just for the future, because i assume that despite it is working, push method is for some reasons better, at least seems to be more sofisticated.

const quizz = [
  ['What is the polar bears skin color?', 'black'],
  ['What kind of horse can see up fron as good as from behind?', 'blind'],
  ['Which wheel is not in use while turning?', 'spare'],
  ['What month has 28 days?', 'every'],
  ['What rocks are in the ocean?', 'wet'],
];
let right = [];
let wrong = [];
let counterGood = 0;
let counterBad = 0;
let i;
function print(message) {
  document.write(message);
}

for (i=0; i<quizz.length; i+=1) {
  question = prompt(quizz[i][0]);
  question = question.toLowerCase();
  let answer = quizz[i][1];
  if (question === answer) {
    counterGood += 1;
    right += quizz[i][0] + '<br>';
  } else {
    counterBad += 1;
    wrong += quizz[i][0] + '<br>';
  }
  }
  print('<h1>You got ' + counterGood + ' questions right and ' + counterBad + ' wrong answers!</h1><br>');
  print('<h3>You answered those correctly:</h3> <br>' + right);
  print('<h3>You answered those incorrectly:</h3> <br>' + wrong);

Thanks!

2 Answers

Steven Parker
Steven Parker
231,275 Points

The variables "right" and "wrong" become strings when you use the "+=" operator to combine them with a string.

The "push" method would allow you to add string items onto them and keep them as arrays, but then you'd need a more sophisticated process to output them at the end.