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

310
Visualizações
Unable to access a property on a variable within a function. Uncaught TypeError:

I have a trivia app that is importing an array of objects called "questions". I have used getElementById to capture each of the buttons, and also added click event listeners for the "checkAnswer" function below.

However, I am getting the error: Uncaught TypeError: can't access property "correct", answer is undefined. This happens with any button I click.

I am using type="module" within the script tag in the HTML file.

The buttons are populating the correct text, but are not console logging any text like it should.

"randomQuestion" is a global variable that is being generated in another function within the same file. This is what it looks like:

  randomQuestion = questions[Math.floor(Math.random()*questions.length)];

function checkAnswer(bntIndex) {
  answer = randomQuestion.answers[bntIndex];
  if (answer.correct === true) {
    console.log("Correct!");
  } else {
    console.log("Incorrect");
  }
}

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

It's rather a warning than an error.

You are getting those because you don't export this function and also don't use it anywhere

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