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

279
Visualizações
HTML / Javascript ReferenceError: Can't find variable

I've made extensive use of the search function and whilst there seem to be many instances of this problem, none are close enough for me to glean a solution to my variant of the problem.

I'm using vanilla Javascript to create a multiplication game. When the button is pressed either by the user pressing 'Enter' or clicking the button the console shows me this error:

ReferenceError: Can't find variable: checkAnswer

Here is the code:

var user_answer;
  for(var x in questions){
    document.getElementById("bronze").innerHTML = '<p>' + questions[x] + '</p>';
    document.getElementById("bronze").innerHTML += '<p><input type="number" id="user_input"></p>';
    document.getElementById("bronze").innerHTML += '<p><button type="button" id="submit_button" onclick="checkAnswer()">Enter</button></p>';
    var user_input = document.getElementById("user_input");
    user_input.addEventListener("keyup", function(event){
      if(event.keyCode == 13){
        event.preventDefault();
        document.getElementById("submit_button").click();
      }
    });
    function checkAnswer(){
      user_answer = document.getElementById("user_input").value;
      if(user_answer == answers[x]){
        alert('Correct!');
      } else {
        alert('Wrong!');
      }
    }
  }

Edit to say that questions and answers are arrays each containing the questions and corresponding answers in the same position. So questions will be like [1x2,2x2,...] and answers [2,4,...]

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

0

Thank you all for such fast replies. You were all correct so I couldn't pick one answer but here's what I did:

  1. Moved the checkAnswer function outside the for loop.
  2. Made 'answers' and 'x' global variables by declaring them outside the function(s).
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