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

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

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 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