summaryrefslogtreecommitdiffstats
path: root/mult.js
blob: 45fc9cd70a20626918756f5282296b1c7cb02c7e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
var TABLE_SIZE = 12;

var startDate, op1, op2, ans;

var testFormEl      = document.getElementById('test-form');
var op1El           = document.getElementById('op1');
var op2El           = document.getElementById('op2');
var answerEl        = document.getElementById('answer');
var isCorrectEl     = document.getElementById('is-correct');
var correctEl       = document.getElementById('correct');
var incorrectEl     = document.getElementById('incorrect');
var correctAnswerEl = document.getElementById('correct-answer');
var submitEl        = document.getElementById('submit');
var timerEl         = document.getElementById('timer');
var timeEl          = document.getElementById('time');

function setUp()
{
    op1 = Math.round(Math.random() * (TABLE_SIZE-1) + 1);
    op2 = Math.round(Math.random() * (TABLE_SIZE-1) + 1);
    ans = op1 * op2;

    op1El.innerHTML           = op1;
    op2El.innerHTML           = op2;
    answerEl.value            = '';
    correctAnswerEl.innerHTML = ans;
    testFormEl.onsubmit       = checkAnswer;

    submitEl.innerHTML        = 'Check';
    timerEl.style.display     = 'none';
    correctEl.style.display   = 'none';
    incorrectEl.style.display = 'none';

    answerEl.focus();

    startDate = new Date();
}

function checkAnswer() {
    var endDate = new Date();
    timeEl.innerHTML = ((endDate - startDate)/1000).toFixed(1);
    timerEl.style.display = 'block';

    if (answerEl.value == ans) {
        correctEl.style.display   = 'inline';
        incorrectEl.style.display = 'none';
    } else {
        incorrectEl.style.display = 'inline';
        correctEl.style.display   = 'none';
    }

    testFormEl.onsubmit = refresh;
    submitEl.innerHTML  = 'Try another';
    answerEl.focus();

    return false;
}

function refresh() {
    setUp();

    return false;
}

setUp();