-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
97 lines (83 loc) · 2.96 KB
/
script.js
File metadata and controls
97 lines (83 loc) · 2.96 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
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
async function getQuoteFile(){
let quotesFileUrl = "https://raw.githubusercontent.com/monkeytypegame/monkeytype/master/frontend/static/quotes/english.json";
return await fetch(quotesFileUrl).then(file => file.text()).then(t => t);
}
async function getQuotes(){
let quotesFileObject = JSON.parse(await getQuoteFile());
return await quotesFileObject.quotes;
}
let textToType = null;
async function getRandomQuote(){
let quotes = await getQuotes();
let quoteId = (Math.random() * 1000 % quotes.length).toFixed(0);
return quotes[quoteId].text;
}
async function loadTextToType(){
textToType = await getRandomQuote();
}
let textTyped = '';
let DOMstatsWpm = document.getElementById("stats-wpm");
let DOMstatsTime = document.getElementById("stats-time");
let DOMstatsAccuracy = document.getElementById("stats-accuracy");
let DOMtextDisplay = document.getElementById("text-display");
let statsIntervalID;
let timerBegin = null;
let time = 0;
resetTextTyped();
document.addEventListener("keydown", (e) => {
e.preventDefault();
if(statsIntervalID == null){
timerBegin = Date.now();
statsIntervalID = setInterval(renderStats, 1000);
}
if(e.code === `Key${e.key.toUpperCase()}`){
textTyped += e.key;
} else if(e.code === "Space"){
if(textTyped[textTyped.length-1] != " ")
textTyped += ` `;
} else if (e.key === "Backspace"){
textTyped = textTyped.substring(0, textTyped.length - 1);
} else if (e.code === "Comma"){
textTyped += `,`;
} else if (e.code === "Period"){
textTyped += `.`;
} else if (e.code === "Tab"){
resetTextTyped();
}
renderTextDisplay();
});
async function resetTextTyped(){
stopTimer();
textTyped = ``;
await loadTextToType();
renderTextDisplay();
}
function stopTimer(){
clearInterval(statsIntervalID);
statsIntervalID = null;
}
function renderStats(){
let timerEnd = Date.now();
time = timerEnd - timerBegin;
let timeInSeconds = time / 1000;
timeInSeconds = timeInSeconds.toFixed(0);
let wpm = (textTyped.length / timeInSeconds ) * 12;
wpm = wpm.toFixed(0);
let accuracy = ((textTyped.length - DOMtextDisplay.getElementsByClassName("incorrect").length) / textTyped.length) * 100;
accuracy = accuracy.toFixed(0);
DOMstatsWpm.innerHTML = "Speed: " + wpm + " wpm";
DOMstatsTime.innerHTML = "Time: " + timeInSeconds + "s";
DOMstatsAccuracy.innerHTML = "Accuracy: " + accuracy + "%";
}
function renderTextDisplay(){
let textToDisplay = '';
for(let i = 0; i < textTyped.length; i++){
let letter = document.createElement("span");
letter.className = textTyped[i] === textToType[i] ? `correct` : `incorrect`;
letter.innerHTML = textTyped[i];
textToDisplay += letter.outerHTML;
}
textToDisplay += `</span><span class="caret" id="caret"></span>`;
textToDisplay += textToType.slice(textTyped.length);
DOMtextDisplay.innerHTML = textToDisplay;
}