index.html 2.61 KB
Newer Older
Kevin Lyda's avatar
Kevin Lyda committed
1
2
3
4
<!DOCTYPE html>
<html>
  <head>
  <meta charset="utf-8" />
Kevin Lyda's avatar
Kevin Lyda committed
5
  <meta name="viewport" content ="width=device-width,initial-scale=1,user-scalable=yes" />
Kevin Lyda's avatar
Kevin Lyda committed
6
7
8
    <title>Cube Root Training</title>
  </head>
  <body>
Kevin Lyda's avatar
Kevin Lyda committed
9
10
  <div id=main>
  <b>Cube Root Training</b>
Kevin Lyda's avatar
Kevin Lyda committed
11
12
13
  <p>
    <span style="white-space:nowrap;"><span style="font-size:larger;"
        >&#8731;</span><span id=cube style="text-decoration:overline;">
Kevin Lyda's avatar
Kevin Lyda committed
14
15
      </span>&nbsp;=&nbsp;
      <input type=number id=cuberoot value="" min=10 max=99>
Kevin Lyda's avatar
Kevin Lyda committed
16
17
    </span>
  </p>
Kevin Lyda's avatar
Kevin Lyda committed
18
19
  <p id=result></p>
  <p id=stats></p>
Kevin Lyda's avatar
Kevin Lyda committed
20
  </div>
Kevin Lyda's avatar
Kevin Lyda committed
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
  <script>
var cube = document.getElementById("cube");
var cuberoot = document.getElementById("cuberoot");
var result = document.getElementById("result");
var stats = document.getElementById("stats");
var began;
var finished;
var elapsed = 0;
var generated;
var guesses = 0;
var correct = 0;

cuberoot.addEventListener("keydown", function (e) {
  if (e.keyCode === 13) {
    check_cuberoot(e);
  }
})

function humanize_ms(ms) {
  if (ms < 1000) {
    return ms + "ms";
  } else {
    return (Math.floor(ms / 100) / 10) + "s";
  }
}

function start_round() {
  if (guesses > 0) {
    stats.innerHTML = "Got " + correct + " correct out of " + guesses +
                      ". Last round took " + humanize_ms(finished - began) +
                      "." + ((correct > 0)
                          ?" Average time of correct guess was " +
                          humanize_ms(elapsed / correct) + "."
                          :"");
  }
  generated = Math.floor(Math.random() * 90) + 10;
Kevin Lyda's avatar
Kevin Lyda committed
57
  cube.innerHTML = "&nbsp;" + Math.pow(generated, 3) + "&nbsp;";
Kevin Lyda's avatar
Kevin Lyda committed
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
  began = Date.now();
  cuberoot.value = "";
  cuberoot.focus();
}

function check_cuberoot(e) {
  var guess = e.target.value;

  guesses++;
  finished = Date.now();
  if (guess == generated) {
    correct++;
    elapsed += (finished - began);
    result.innerHTML = "Correct!";
  } else {
Kevin Lyda's avatar
Kevin Lyda committed
73
74
75
76
77
    result.innerHTML = 'Not correct! <span style="white-space:nowrap;">' +
      '<span style="font-size:larger;">&#8731;</span>' +
      '<span style="text-decoration:overline;">&nbsp;' +
      Math.pow(generated, 3) +
      '&nbsp;</span> = ' + generated + '</span>.';
Kevin Lyda's avatar
Kevin Lyda committed
78
79
80
81
  }
  start_round();
}

82
function make_text_readable() {
83
  if (screen.width <= 480) {
84
    document.body.style.fontSize = "8vw";
85
    cuberoot.style = "font-size:8vw;";
86
  } else if (screen.width <= 1024) {
87
    document.body.style.fontSize = "6vw";
88
89
90
91
    cuberoot.style = "font-size:6vw;";
  } else {
    document.body.style.fontSize = "48px";
    cuberoot.style = "font-size:48px;";
92
  }
Kevin Lyda's avatar
Kevin Lyda committed
93
94
}

95
96
97
window.onload = make_text_readable;
window.onresize = make_text_readable;

Kevin Lyda's avatar
Kevin Lyda committed
98
99
100
101
start_round();
  </script>
  </body>
</html>