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

311
Views
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 answers
Answer question

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