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>
Aucun commentaire:
Enregistrer un commentaire