【问题标题】:How do I check answers in array objects by its index?如何通过索引检查数组对象中的答案?
【发布时间】:2022-01-04 09:15:25
【问题描述】:

我正在尝试用 JavaScript 构建一个测验。我有一个带有问题、答案和正确答案的对象。我可以通过将答案中的索引与正确答案的索引进行匹配来检查用户是否正确回答了问题。

但是,当我尝试使用多个对象并尝试运行此代码时,它不起作用。我怎样才能做到这一点?`

这行得通:

let questions = {
  question: "How many sides does a square have?",
  answers: [4, 6, 8],
  correctAnswer: 0,
  category: "trueOrFalse"
};

const iterator = questions.answers.keys();
for (const key of iterator) {
  if (key === questions.correctAnswer) {
    console.log(questions.question + ": " + questions.answers[key]);
  }
}

但这不起作用:

let questions =
  {
    question: "How many sides does a square have?",
    answers: [4, 6, 8],
    correctAnswer: 0
  },
  {
    question: "How many sides does a triangle have?",
    answers: [3, 6, 8],
    correctAnswer: 0
  }
;

const iterator = questions.answers.keys();
for (const key of iterator) {
  if (key === questions.correctAnswer) {
    console.log(questions.question + ": " + questions.answers[key]);
  }
}

这是我的仓库的链接:DC Quiz

当我尝试回答问题 4 时出现问题。在我的代码中,您可以在第 206 行的 main.js 文件中看到它有问题。

感谢您的帮助! 米歇尔

【问题讨论】:

    标签: javascript object indexing


    【解决方案1】:

    如上所述,您需要在数组中定义您的问题。 喜欢:

    const questions = [{
        question: "How many sides does a triangle have?",
        answer: [3, 5, 7],
        correctAnswer: 0
      },
      {
        question: "some question?",
        answer: [1, 42, "another answer"],
        correctAnswer: 2
      }
    ]
    

    您可能比使用 .keys() 更难,这个数组函数返回一个带有索引号而不是实际值的迭代器,请阅读它here

    由于您将索引存储到正确答案,您可以通过循环遍历您的问题数组并处理如下答案来轻松解决此问题:

    for (let q of questions) {
      console.log(q.question, ": ", answers[correctAnswer])
    }
    

    这是有效的,因为 correctAnswer 持有正确答案的索引键。

    【讨论】:

      【解决方案2】:

      我想,首先您可以遍历一系列问题,然后再检查您的答案。但是,在我看来,在您的回购结构中,问题的结构有点模棱两可。尚不清楚答案是否包含测验的答案集(如果是,为什么要检查此列表以获取正确答案?我的意思是,我想,无论如何,该集都应包含正确答案)或用户答案的​​索引(如果是,则应检查值正确答案属性中的索引相对于答案数组中的值)。

      for (const question of questions) {
          if ([check your answers with question.answers and question.correctAnswer]) {
              [output: something like question.answers[question.correctAnswer]]
          }
      }
      

      【讨论】:

        【解决方案3】:

        您还必须遍历您的问题数组。 第一个问题在:

        questions[0].question
        

        顺便说一句: 你的问题变量应该是一个数组。 (喜欢你的答案) 尝试以下方法:

            let questions =
          [{
            question: "How many sides does a square have?",
            answers: [4, 6, 8],
            correctAnswer: 0
          },
          {
            question: "How many sides does a triangle have?",
            answers: [3, 6, 8],
            correctAnswer: 0
          }
        ;
        
        const iterator = questions.answers.keys();
        for (const key of iterator) {
          if (key === questions.correctAnswer) {
            console.log(questions.question + ": " + questions.answers[key]);
          }
        }]
        

        说明:

        问题数组捆绑了所有问题对象。如果你想问一个特定的问题,你需要告诉 javascript 你想问的是哪个问题对象。

        是第一个问题吗?:

        question[0].question
        

        意思:“正方形有几条边?”

        或者可能是另一个?

        【讨论】:

          【解决方案4】:

          您需要将问题定义为对象数组。

          let questions = [
            {
              question: "How many sides does a square have?",
              answers: [4, 6, 8],
              correctAnswer: 0
            },
            {
              question: "How many sides does a triangle have?",
              answers: [3, 6, 8],
              correctAnswer: 0
            }
          ];
          

          【讨论】:

            【解决方案5】:

            将您的问题作为一个对象数组并对其进行迭代,就像这里一样简单:

            let questions = [
              {
                question: "How many sides does a square have?",
                answers: [4, 6, 8],
                correctAnswer: 0
              },
              {
                question: "How many sides does a triangle have?",
                answers: [3, 6, 8],
                correctAnswer: 0
              }]
            ;
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2017-03-12
              • 1970-01-01
              • 2020-05-29
              相关资源
              最近更新 更多