Home > Enterprise >  Problem checking if user input (answer to a quiz question) matches one of the correct answers in my
Problem checking if user input (answer to a quiz question) matches one of the correct answers in my

Time:09-27

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]. If the user inputs either of these answers, the function should return true and add up to the total amount of correctly answered questions. 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:

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:

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. I read that .includes does not work on nested arrays, is this true? 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.

However, when reaching the nested array with multiple possible answers (e.g. German car brands), my correctAnswers.includes doesn't work/add to the total 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!

CodePudding user response:

You Can Use indexOf to check string and arrays both works exact as includes see example below

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)

  • Related