简体   繁体   English

检查用户输入(测验问题的答案)是否与我的嵌套数组中的正确答案之一相匹配时出现问题 using.includes

[英]Problem checking if user input (answer to a quiz question) matches one of the correct answers in my nested array using .includes

Goal: My goal is to compare user input (answer to a quiz question) with a list of correct answers in a (nested) array.目标:我的目标是将用户输入(测验问题的答案)与(嵌套)数组中的正确答案列表进行比较。 For example, the question could be: Name a German car brand, and the answer can be either of these: ["Volkswagen", "Audi", "Opel", "Porsche" etc].例如,问题可能是:说出一个德国汽车品牌,答案可以是以下之一:[“Volkswagen”、“Audi”、“Opel”、“Porsche”等]。 If the user inputs either of these answers, the function should return true and add up to the total amount of correctly answered questions.如果用户输入这些答案中的任何一个,则 function 应返回 true 并将正确回答的问题总数相加。 If all questions are correctly answered, I want to end the code with a message for the user:如果所有问题都得到正确回答,我想用一条给用户的消息结束代码:

if (numberCorrect == quizQuestions.length) {
        document.getElementById("correct").innerHTML = "All answers are correct";

Code of quizQuestions array: quizQuestions 数组的代码:

const quizQuestions = [
    "Wat is de hoofdstad van Frankrijk?",
    "Hoeveel benen heeft een spin?",
    "Wat is het grootste meer van Nederland?",
    "Noem een Duits automerk",
    "Noem een Waddeneiland",
    "In hoeveel dagen draait de aarde om de zon?",
    "Wat is de grootste oceaan ter wereld?"
];

Code of correctAnswers array: correctAnswers 数组的代码:

const correctAnswers = [
    "Parijs",
    8,
    "IJsselmeer",
    ["Volkswagen", "Audi", "Opel", "Porsche", "BMW", "Mercedes", "Mercedes-Benz"],
    ["Texel", "Vlieland", "Terschelling", "Ameland", "Schiermonnikoog"],
    365,
    "Stille Oceaan"
];

Problem: For some reason, going through the two nested answer arrays using correctAnswers.includes(answer) does not work.问题:出于某种原因,使用 correctAnswers.includes(answer) 遍历两个嵌套答案 arrays 不起作用。 I read that.includes does not work on nested arrays, is this true?我读到 .includes 不适用于嵌套的 arrays,这是真的吗? My other quiz questions with one answer, such as: What is the capital of France, are easily compared with the user input using this basic code:我的其他只有一个答案的测验问题,例如:法国的首都是什么,可以很容易地与使用此基本代码的用户输入进行比较:

numberCorrect = 0;

    var answer = document.querySelector("#input0").value;
    if (correctAnswers[0] == answer) {
        numberCorrect++; 
    } 

The code above correctly adds 1 to correctAnswers.上面的代码正确地将 1 添加到 correctAnswers。

However, when reaching the nested array with multiple possible answers (eg German car brands), my correctAnswers.includes doesn't work/add to the total numberCorrect:但是,当到达具有多个可能答案(例如德国汽车品牌)的嵌套数组时,我的 correctAnswers.includes 不起作用/添加到总数 numberCorrect:

  answer = document.querySelector("#input3").value;
    if (correctAnswers.includes(answer) == true) {
        numberCorrect++;
    } 

Also, It seems I am not allowed to be more precise and pick the right index in the parent array like this:此外,似乎我不能更精确地在父数组中选择正确的索引,如下所示:

correctAnswers[3].includes(answer) 

Is there perhaps anyone that can help out?也许有人可以帮忙吗? Thanks in advance!提前致谢!

You Can Use indexOf to check string and arrays both works exact as includes see example below您可以使用indexOf来检查字符串和 arrays 两者都与includes的内容完全一样,请参见下面的示例

 const correctAnswers = [ "Parijs", 8, "IJsselmeer", ["Volkswagen", "Audi", "Opel", "Porsche", "BMW", "Mercedes", "Mercedes-Benz"], ["Texel", "Vlieland", "Terschelling", "Ameland", "Schiermonnikoog"], 365, "Stille Oceaan" ]; var isAnsRight = correctAnswers[3].indexOf("Audi") >= 0 console.log(isAnsRight)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM