Document
Document
var score;
var timeremaining;
var countdown;
var correctAns;
document.body.innerHTML = `
<div id="choices">
<div id="box1" class="box text-center" style="border: 1px solid #000; display: inline-block; width:
100px; height: 100px; line-height: 100px; margin: 10px; cursor: pointer;"></div>
<div id="box2" class="box text-center" style="border: 1px solid #000; display: inline-block; width:
100px; height: 100px; line-height: 100px; margin: 10px; cursor: pointer;"></div>
<div id="box3" class="box text-center" style="border: 1px solid #000; display: inline-block; width:
100px; height: 100px; line-height: 100px; margin: 10px; cursor: pointer;"></div>
<div id="box4" class="box text-center" style="border: 1px solid #000; display: inline-block; width:
100px; height: 100px; line-height: 100px; margin: 10px; cursor: pointer;"></div>
</div>
</div>
`;
// Register Events
document.getElementById("startreset").addEventListener("click", play);
document.getElementById("box1").addEventListener("click", checkAnswer);
document.getElementById("box2").addEventListener("click", checkAnswer);
document.getElementById("box3").addEventListener("click", checkAnswer);
document.getElementById("box4").addEventListener("click", checkAnswer);
function play(e) {
window.location.reload();
} else {
playing = true;
score = 0;
setText("scoreValue", score);
hide("gameover");
timeremaining = 60;
setText("timeremainingValue", timeremaining);
show("timeremaining");
startCountdown();
generateQA();
function generateQA() {
var x = (1 + Math.round(Math.random() * 9));
correctAns = x * y;
setText("question", quesString);
setText(`box${correctPos}`, correctAns);
var wrongAns;
if (i != correctPos) {
do {
setText(`box${i}`, wrongAns);
answers.push(wrongAns);
function checkAnswer() {
if (this.innerHTML == correctAns) {
score++;
setText("scoreValue", score);
show("correct");
hide("wrong");
setTimeout(function () {
hide("correct");
}, 500);
generateQA();
} else {
show("wrong");
hide("correct");
setTimeout(function () {
hide("wrong");
}, 500);
function startCountdown() {
countdown = setInterval(function () {
timeremaining -= 1;
setText("timeremainingValue", timeremaining);
if (timeremaining <= 0) {
clearInterval(countdown);
playing = false;
show("gameover");
hide("timeremaining");
setText("scoreValue", "");
setText("gameover", msg);
}, 1000)
}
// Helper Functions
document.getElementById(id).innerHTML = text;
function show(id) {
document.getElementById(id).style.display = 'block';
function hide(id) {
document.getElementById(id).style.display = 'none';