Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

280
Views
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]. 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!

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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)

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!