jeudi 20 février 2020

JS possible if statement issue [duplicate]

I've been trying to debug this JS code for quite a while now and was wondering if anyone could help. personally think it is a problem in my if statement but I am not 100% sure. It might also be a problem with my HTML code but I am doubtful it is.

window.onload = function() {

  var x = Math.floor(Math.random() * 10);
  var y = Math.floor(Math.random() * 10);
  var ans = x * y;

  document.getElementById("question");
  document.getElementById("giveQuestion");
  document.getElementById("ansCheck");
  document.getElementById("correctIn");

  function getValue() {
    givenAnswer = document.getElementById("ansVal").value;
  }

  giveQuestion.addEventListener('click', function() {
    question.innerText = (x + ' ' + 'X' + ' ' + y);
    console.log(x);
    console.log(y);
    console.log(ans);
  })

  ansCheck.addEventListener('click', function() {
    getValue();
    if (givenAnswer === ans) {
      correctIn.innerText = "Horray You Got It Right!";
    } else {
      correctIn.innerText = "Oh no, You got it wrong. Try again.";
    }
  })

}
<!DOCTYPE html>
<html>

<head>
  <Title>Math</Title>
  <script type="text/javascript" src="main.js"></script>
</head>

<body>
  <button id="giveQuestion">Give Me A Question!</button>
  <br>
  <p id="question"></p>
  <br>
  <input type="number" id="ansVal">
  <br>
  <button id="ansCheck">Check Your Answer</button>
  <br>
  <p id="correctIn"></p>
</body>

</html>
I am a pretty decent HTML coder but I am just learning JS so I am pretty sure that the problem lies in the JS

Aucun commentaire:

Enregistrer un commentaire