简单的JavaScript测验数组问题

时间:2016-05-27 10:32:13

标签: javascript

我是一名编程新手,正在通过Treehouse的测验。我不想只看解决方案但是我被卡住了。我想将每个正确的问题存储在一个新的数组中,并为每个错误的问题存储相同的问题,然后将它们打印出来。我的代码会跟踪每个正确和错误的问题,但它只为每个新数组保存一个问题,即使多个正确或不正确也是如此。我确定它很简单,但我做错了什么?



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);
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:3)

  

使用array来保存questions而不是变量

join()方法将数组的所有元素连接成一个字符串。

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

答案 1 :(得分:0)

作为首发,而不是为正确和错误的答案重新声明变量。每次回答时将问题推送到变量上:

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);