【问题标题】:Reset random array item as new variable (javascript)将随机数组项重置为新变量(javascript)
【发布时间】:2020-09-24 22:03:02
【问题描述】:

我从数组中提取了一个随机问题,从数组中的数组中提取了三个可能的答案。正确答案基于数组中可能答案在数组中的位置,并与 prompt() 中的用户输入进行比较。

ie:[1[correct, x, x], 2[x, correct, x], 3[x, x, correct]. 

对于第一个随机问题,答案编号与正确答案匹配。在第一个问题之后,程序接受的答案不再与用户输入的正确答案匹配。无论随机问题的顺序如何,都是如此。

例如:

Question 2 (displayed first):
1) x 
2) y - correct
3) z

Question 1 (displayed next):
1) a - correct but returns false
2) b - wrong but returns correct
3) c

这是我的代码。输入'quit'结束提示框弹出。

    var Question = function(question, answerArray, answer) {
      this.question = question;
      this.answerArray = answerArray;
      this.answer = answer;
    };

    Question.prototype.questionPrompt = function() {
      console.log(this.question);
      this.answerArray.forEach(function(answerList, index) {
        console.log(index + ') ' + answerList)
      });
    };

    var questionArray = new Array(
      q1 = new Question('Question A:', ['A', 'B', 'C'], 'A'),
      q2 = new Question('Question B:', ['A', 'B', 'C'], 'B'),
      q3 = new Question('Question C:', ['A', 'B', 'C'], 'C')
    )

    var randomQ = Math.floor(Math.random() * questionArray.length);

    questionArray[randomQ].questionPrompt();

    Question.prototype.answerPrompt = function() {
      var guess = prompt("Enter number of the correct answer.");
      var currentQ = randomQ;
      var tryQ = guess && Number(guess);
      if (currentQ === tryQ) {
        console.log('Correct! ' + this.answer);
        nextQ();
      } else if (guess === "quit") {
    console.log("Goodbye.")
  } else {
        console.log('Try again.' + this.answer);
        nextQ();
      }
    };

    questionArray[randomQ].answerPrompt();

    var newQ;

    function nextQ() {
      newQ = Math.floor(Math.random() * questionArray.length);
      questionArray[newQ].questionPrompt();
      questionArray[newQ].answerPrompt();
    };

是什么导致 newQ() 中问题的数组位置与 newQ() 中的答案不同,而 randomQ 的位置保持不变?

【问题讨论】:

  • 如果您可以使您的脚本可运行显示我们可以自己完成每一步,这将有很大帮助。在问题编辑器中查找 <> 图标以创建 sn-p。
  • 不知道randomQ 来自哪里。如果randomQ 与页面上问题的顺序有关,而不是在数组中,则它们将不匹配。这是一个很好的例子,说明为什么应该避免基于位置的逻辑,因为它既令人困惑又不必要地脆弱。

标签: javascript


【解决方案1】:

试试这个:

Question.prototype.answerPrompt = function() {
  var guess = prompt("Enter number of the correct answer.");
  var currentQ = randomQ;
  var tryQ = guess && Number(guess);
  if (this.answerArray[tryQ + 1] === this.answer) { // answersArray starts at 0
    console.log('Correct! ' + this.answer);
    nextQ();
  } else {
    console.log('Try again.');
    nextQ();
  }
};

您正在将随机问题的索引与不相关的所选答案的索引进行比较。您应该检查所选答案是否等于问题答案。

【讨论】:

  • 使用此代码,它似乎根本无法识别正确答案。有时问题和答案会在 0 位置,所以我很难理解将 +1 添加到 tryQ。你能解释一下这背后的逻辑吗?
【解决方案2】:

这是在全局范围内定义变量以及调用变量的顺序的问题。 randomQ 必须在 init() 函数内定义以重置问题数组的编号,从而使 newQ 变量变得多余。

步骤1)在全局范围内定义变量:

var randomQ; 
var guess;

步骤 2) 创建init() 函数并在其中移动随机数代码。在全局范围内调用 init:

function init(){
  randomQ = Math.floor(Math.random() * questionArray.length);
  questionArray[randomQ].questionPrompt();
  questionArray[randomQ].answerPrompt();
}

init();  

步骤 3) 将提示从 answerPrompt 移至 questionPrompt,并在 answerPrompt 中的每个 if/else 语句的末尾调用 init()

// select random question here
  Question.prototype.questionPrompt = function() {
    console.log(this.question);
    this.answerArray.forEach(function(answerList, index) {
      console.log(index + ') ' + answerList)
    });
    guess = prompt("Enter number of the correct answer.");
  };

  // // CHECKS FOR CORRECT ANSWER and prints to console
  Question.prototype.answerPrompt = function() {
    var tryQ = guess && Number(guess);
    if (randomQ === tryQ) {
      console.log('Correct! ' + this.answer);
      init();
    } else if (guess === "quit") {
      console.log("Goodbye.")
    } else {
      console.log('Try again.');
      init();
    };
  };

【讨论】:

    猜你喜欢
    • 2014-03-28
    • 1970-01-01
    • 2022-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-20
    相关资源
    最近更新 更多