-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
56 lines (45 loc) · 1.49 KB
/
script.js
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
const keys = [..."ABCDEFGHIJKLMNOPQRSTUVWXYZ"];
const timestamps = [];
timestamps.unshift(getTimestamp());
function getRandomNumber(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1)) + min;
}
function getRandomKey() {
return keys[getRandomNumber(0, keys.length-1)]
}
function targetRandomKey() {
const key = document.getElementById(getRandomKey());
key.classList.add("selected");
let start = Date.now()
}
function getTimestamp() {
return Math.floor(Date.now() / 1000)
}
document.addEventListener("keyup", event => {
const keyPressed = String.fromCharCode(event.keyCode);
console.log(keyPressed)
const keyElement = document.getElementById(keyPressed);
console.log(keyElement)
const highlightedKey = document.querySelector(".selected");
console.log(highlightedKey)
keyElement.classList.add("hit")
keyElement.addEventListener('animationend', () => {
keyElement.classList.remove("hit")
})
if (keyPressed === highlightedKey.innerHTML) {
timestamps.unshift(getTimestamp());
const elapsedTime = timestamps[0] - timestamps[1];
console.log(`Character per minute ${60/elapsedTime}`)
highlightedKey.classList.remove("selected");
targetRandomKey();
const success = new Audio('sounds/correct.wav');
success.play();
} else {
let randNum = Math.floor(Math.random()*8) + 1;
const errorAudio = new Audio(`sounds/error${randNum}.mp3`);
errorAudio.play();
}
})
targetRandomKey();