-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.js
120 lines (101 loc) · 3.72 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
"use strict";
// Grabbing html elements
//Buttons
const startButton = document.getElementById("start-button");
const retryButton = document.getElementById("retry-button");
// Typing content
const textDiv = document.getElementById("text");
// Sounds
const clickSound = new Audio("sounds/buttonClick.mp3");
// Stats elements
const statsDiv = document.getElementById("stats");
const time = document.getElementById("time-elapsed");
const wordsPerMin = document.getElementById("wpm");
const accuracy = document.getElementById("accuracy");
const correctInput = document.getElementById("correctInput");
const incorrectInput = document.getElementById("incorrectInput");
// Hiding elements before game start
textDiv.classList.add("hide");
statsDiv.classList.add("hide");
retryButton.classList.add("hide");
// get a random quote
let quote;
async function getQuote() {
const results = await fetch("https://api.quotable.io/random");
const data = await results.json();
quote = data.content;
}
getQuote();
// Wrapping everything in a start button listener
startButton.addEventListener("click", function () {
clickSound.play();
// Displaying elements, hiding start button
textDiv.classList.remove("hide");
retryButton.classList.remove("hide");
startButton.classList.add("hide");
// Split text into characters
const textIntoCharacters = quote.split("");
const characters = textIntoCharacters.map((char) => {
const span = document.createElement("span");
span.innerText = char;
textDiv.appendChild(span);
return span;
});
// Typing - Starting positition
let cursorPosition = 0;
let cursorCharacter = characters[cursorPosition];
cursorCharacter.classList.add("cursor");
// Tracking right/wrong input just for statistics
let wrongLetters = 0;
let correctLetters = 0;
// Setting initial time to null
let startTime = null;
let endTime = null;
// Listening for button press, starting the time
const keypress = ({ key }) => {
// Time only starts when user starts typing
if (!startTime) {
startTime = new Date();
}
if (key === cursorCharacter.innerText) {
cursorCharacter.classList.remove("cursor");
cursorCharacter.classList.remove("wrong");
cursorCharacter.classList.add("done");
cursorCharacter = characters[++cursorPosition];
correctLetters++;
} else {
cursorCharacter.classList.add("wrong");
cursorCharacter.classList.remove("done");
cursorCharacter.classList.remove("cursor");
cursorCharacter = characters[cursorPosition];
wrongLetters++;
}
// Calculating stats, ending time
if (cursorPosition >= characters.length) {
endTime = new Date();
const timeElapsed = endTime - startTime;
const seconds = (timeElapsed / 1000).toFixed(1);
const wordNumber = quote.split(" ").length;
const wps = (wordNumber / seconds).toFixed(1);
const wpm = (wps * 60).toFixed(1);
// Removing key listening at game end
document.removeEventListener("keypress", keypress);
// Hide text when game is done
textDiv.classList.add("hide");
// Stats that are displayed at game end
correctLetters -= 2 * wrongLetters;
statsDiv.classList.remove("hide");
time.textContent = `${seconds}s`;
wordsPerMin.textContent = `${wpm}`;
accuracy.textContent = `${(
(correctLetters / characters.length) *
100
).toFixed(1)}%`;
correctInput.textContent = ` ${correctLetters}`;
incorrectInput.textContent = `${wrongLetters}`;
return;
}
cursorCharacter.classList.add("cursor");
};
document.addEventListener("keypress", keypress);
});