-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtypingLetter.js
More file actions
45 lines (39 loc) · 1.14 KB
/
Copy pathtypingLetter.js
File metadata and controls
45 lines (39 loc) · 1.14 KB
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
var Score = 0;
var Attempted = 0;
function main() {
let randomLetter = getRandomLetter(65, 90);
document.getElementById("display").innerText = randomLetter;
document.getElementById("score").innerText = Score;
document.getElementById("attempt").innerText = Attempted;
}
function keyPressed(event) {
let randomLetter = document.getElementById("display").innerText;
let userInput = event.key.toUpperCase();
Score += (userInput == randomLetter) ? 10 :-10;
Attempted++;
gameOver();
main();
}
function gameOver() {
let time = document.getElementById('timer').innerText;
if (time == 0) {
document.getElementById('body').onkeypress='';
document.getElementById("over").style="visibility:visible";
document.getElementById("accuracy").innerText = getAccuracy();
}
}
function getAccuracy() {
return Math.floor(Score/Attempted*10);
}
function getRandomLetter(min, max) {
return String.fromCharCode(Math.floor(Math.random() * (max - min + 1) + min));
}
function timer(time) {
setInterval(function(){
if (time > 0) {
time--;
document.getElementById("timer").innerText = time;
}
},1000)
}
window.onload = timer(20);