Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

281
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda