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

Doesnt fully work

Hi,

this is my solution. https://pastebin.com/va7EgET4

There is a problem which I can't solve. The correct answers always show 0. I don't know what I do wrong.

2 Answers

Hello! I solved it.

There are two problems. Both are with the "game" function. Common problems actually...

  1. You are declaring your correctAnswer variable inside of the loop, meaning it resets to 0 everytime you loop(every question).
  2. You are using a string for your correctAnswer variable so it is just appending a "1" to the string answer.

The following game function will work. :)

// Quiz Game
function game(array) {

  var correctAnswers = 0;
  for ( i = 0; i < 3; i += 1) {
    var userAnswer = prompt(array[i][0]).toLowerCase();
  if (userAnswer === array[i][1].toLowerCase() ){
    correctAnswers += 1;
    array[i].push(true);
  } else if (userAnswer !== array[i][1].toLowerCase()){
    array[i].push(false);
  }
    console.log('Correct answers count: ' + correctAnswers);
  }
document.write('<p>You correctly answered <strong>' + correctAnswers + '</strong> questions.</p>');
document.write('<p><strong>You answered the following questions correct:</strong></p>');
document.write('<ol>');
for ( i = 0; i < 3; i += 1) {
  if ( array[i][2] ) {
    document.write('<li>' + array[i][0] + '</li>');
  }
}
document.write('</ol>');
document.write('<p><strong>You ansered the following questions wrong:</strong></p>');
document.write('<ol>');
for ( i = 0; i < 3; i += 1) {
  if ( ! array[i][2] ) {
    document.write('<li>' + array[i][0] + '</li>');
  }
}
document.write('</ol>');
}

Thank you very much. I totally understand what you mean. I should have known that if I declare it to 0 in the loop that it will always reset itself.

Thx and have a nice day!

Eh, we all have done it. Part of being a developer is making mistakes and learning from them. :)

Have a great day!