Last active
September 24, 2022 19:24
-
-
Save travc/fdcd504ec683472b6727b90d6a3621f6 to your computer and use it in GitHub Desktop.
multiplication quiz in basic HTML+Javascript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE HTML> | |
<html> | |
<head> | |
<title> multiplication flashcard </title> | |
<script> | |
// shared variables | |
var term1 = 0; | |
var term2 = 0; | |
var score = 0; | |
var num_tries = 0; | |
// The main part of the program | |
function myMain(event){ | |
document.getElementById("topbox").innerHTML = "<h1> HELLO </h1>"; | |
newProblem(); | |
document.getElementById('answertext').addEventListener('keydown', (ev) => { | |
if (ev.key === 'Enter') { | |
checkAnswer(ev); | |
} | |
}); | |
} | |
function newProblem(){ | |
term1 = Math.floor(Math.random()*13); | |
term2 = Math.floor(Math.random()*13); | |
document.getElementById("probbox").innerHTML = "<h1>"+term1+"×"+term2+"</h1>"; | |
document.getElementById('answertext').value = ""; | |
updateScore(); | |
} | |
function updateScore(){ | |
document.getElementById("scorebox").innerHTML = "<h3> Score = "+100*(score/num_tries)+" %</h3>"; | |
document.getElementById("scorebox").innerHTML += score+" right out of "+num_tries+" tries"; | |
} | |
function checkAnswer(ev){ | |
val = parseInt(ev.target.value); | |
num_tries = num_tries + 1; | |
if( val == term1*term2 ){ | |
document.getElementById("topbox").innerHTML = "<h1>CORRECT</h1>"; | |
score = score + 1; | |
newProblem(); | |
}else{ | |
document.getElementById("topbox").innerHTML = "<h1>WRONG</h1>"; | |
updateScore(); | |
} | |
} | |
// Tell the page to run myMain after the page (HTML) is loaded | |
document.addEventListener('DOMContentLoaded', myMain, false); | |
</script> | |
</head> | |
<body> | |
<!-- This is what you see --> | |
<div id="topbox"></div> | |
<div id="scorebox"></div> | |
<div id="probbox"></div> | |
<div id="answerbox"> | |
<input id="answertext" type="text" id="ans"> | |
</div> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment