// Countdown timer (in seconds)
var countdown = 11;
// ID to track the setTimeout
var id = null;
var punteggio = 0;
var f1 = 0;
var f2 = 0;
//
document.getElementById("riprova").addEventListener("click", start);
// if the paragraph in the "splash screen" is hit, the game is initiated
function start() {
// show game screen
document.getElementById("gioco").style.display = 'block';
// hide splash screen
document.getElementById("splash").style.display = 'none';
// set up timer
setInterval(conto, 1000);
conto();
// start actual game
gioco();
}
// timer function
function conto() {
// countdown
countdown--;
// update countdown in game screen
document.getElementById("timer").innerHTML = countdown;
// if time expires go to gameover function
if (countdown < 0) {
fine();
}
}
// game "loop"
function gioco() {
// hide comment (does not seem to work)
document.getElementById("commento").style.visibility = 'hidden';
// update score
document.getElementById("punteggio").innerHTML = punteggio;
// clear text field
document.getElementById("risposta").value = '';
// pick two random integers between 2 and 12
f1 = Math.floor(Math.random() * 11) + 2;
f2 = Math.floor(Math.random() * 11) + 2;
// display the multiplication
document.getElementById("domanda").innerHTML = f1 + ' × ' + f2 + ' = ';
// focus on text field
document.getElementById("risposta").focus();
}
// wait for the player to hit "enter"
function inputKeyUp(e) {
e.which = e.which || e.keyCode;
if (e.which == 13) {
// convert text field content into an integer
var risp = parseInt(document.getElementById("risposta").value);
// check answer
if (risp == f1 * f2) {
// correct answer
punteggio++;
countdown += 2;
// comment answer (does not seem to work)
document.getElementById("commento").innerHTML = ' correct!';
document.getElementById("commento").style.visibility = 'visible';
// green-light...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Fiddle Pages
Your fiddles accessible under a custom domain and a vanity path - the simplest hosting for your personal project.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Get Fiddle Pages with PRO
Your fiddles accessible under a custom domain and a vanity path - the simplest hosting for your personal project.