Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

282
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda