-1

I'm a programming newbie and am working through a quiz from Treehouse. I don't want to just look at the solution but I'm stuck. I want to store each correct question in a new array and the same for each wrong question, then print them out. My code keeps track of each right and wrong question, but it's only saving one question to each new array, even if more than one is correct or incorrect. I'm sure it's something simple, but what am I doing wrong?

var questions = [
  ['How many states are in the United States?', '50'],
  ['How many legs does a spider have?', '8'],
  ['How many continents are there?', '7']
];

function quiz(quizQuestions) {
  var counter = 0;
  
  for (var i = 0; i < questions.length; i++) {
    var answer = prompt(questions[i][0]);
    
    if (answer === questions[i][1]) {
      var correctAnswers = [questions[i][0]];
      counter += 1;
    } else {
      var wrongAnswers = [questions[i][0]];
    }
  }

  print('<h2>You got these questions right</h2>');
  print(correctAnswers);
  print('<h2>You got these questions wrong</h2>');
  print(wrongAnswers);
  
  var printQuestionsRight = '<h3>You got ' + counter + ' questions right</h3>';
  print(printQuestionsRight);
}

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

quiz(questions);

1
  • Keep it in array than variable Commented May 27, 2016 at 10:33

2 Answers 2

3

Use array to hold the questions than variable

The join() method joins all elements of an array into a string.

var questions = [
  ['How many states are in the United States?', '50'],
  ['How many legs does a spider have?', '8'],
  ['How many continents are there?', '7']
];
var correctAnswers = [];
var wrongAnswers = [];

function quiz(quizQuestions) {
  var counter = 0;
  for (var i = 0; i < questions.length; i++) {
    var answer = prompt(questions[i][0]);
    if (answer === questions[i][1]) {
      correctAnswers.push([questions[i][0]]);
      counter += 1;
    } else {
      wrongAnswers.push([questions[i][0]]);
    }
  }
  print('<h2>You got these questions right</h2>');
  print(correctAnswers.join('<br>'));
  print('<h2>You got these questions wrong</h2>');
  print(wrongAnswers.join('<br>'));
  var printQuestionsRight = '<h3>You got ' + counter + ' questions right</h3>';
  print(printQuestionsRight);
}

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

Fiddle Demo

Sign up to request clarification or add additional context in comments.

1 Comment

@Austin, I'm glad it helped! Happy Coding
0

As a starter, instead of re-declaring variables for the correct and incorrect answers. Push the questions onto the variables each time they answer:

var questions = [
  ['How many states are in the United States?', '50'],
      ['How many legs does a spider have?', '8'],
      ['How many continents are there?', '7']
    ],
    correctAnswers = [],
    wrongAnswers = [];

    function quiz(quizQuestions) {
      var counter = 0;
      
      for (var i = 0; i < questions.length; i++) {
        var answer = prompt(questions[i][0]);
        
        if (answer === questions[i][1]) {
          correctAnswers.push ([questions[i][0]]);
          counter += 1;
        } else {
          wrongAnswers.push ([questions[i][0]]);
        }
      }

      print('<h2>You got these questions right</h2>');
      print(correctAnswers);
      print('<h2>You got these questions wrong</h2>');
      print(wrongAnswers);
      
      var printQuestionsRight = '<h3>You got ' + counter + ' questions right</h3>';
      print(printQuestionsRight);
    }

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

    quiz(questions);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.