-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
108 lines (94 loc) · 2.67 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
const QUOTELISTURL = "https://type.fit/api/quotes";
let random = Math.floor(Math.random() * 1000);
let increment = 0;
let typedchars = 0;
let time = 0;
let wpm = 0;
quoteInput.addEventListener("input", (e) => {
const quotearray = quoteDisplay.querySelectorAll("span");
const inputarray = quoteInput.value.split("");
increment = quoteInput.value.length;
if (
e.inputType != "deleteContentBackward" ||
e.inputType != "insertLineBreak"
) {
typedchars = 0;
console.log("error");
}
if (quotearray[increment] != undefined)
quotearray[increment].classList.add("highlight");
quotearray.forEach((quotechar, index) => {
const inputchar = inputarray[index];
if (inputchar == null) {
quotechar.classList.remove("correct");
quotechar.classList.remove("wrong");
} else if (inputchar === quotechar.innerText) {
quotechar.classList.remove("highlight");
quotechar.classList.add("correct");
quotechar.classList.remove("wrong");
} else {
quotechar.classList.remove("highlight");
quotechar.classList.remove("correct");
quotechar.classList.add("wrong");
}
});
if (quoteInput.value.length == quotearray.length) {
window.location.reload();
}
});
async function getAllQuotes() {
const response = await fetch(QUOTELISTURL);
const data = await response.json();
return data;
}
async function renderQuote() {
//init
let quotelist = await getAllQuotes();
let quote = quotelist[random].text;
let author = "";
author =
quotelist[random].author != null
? (author += quotelist[random].author)
: "Anonymous";
// create spans for each character
quote.split("").forEach((char) => {
let charspan = document.createElement("span");
charspan.innerText = char;
quoteDisplay.appendChild(charspan);
});
//append author
let authorDiv = document.createElement("p");
authorDiv.innerText = author;
body.appendChild(authorDiv);
//other
quoteInput.value = null;
startTimer();
}
function startTimer() {
//timer.innerText = 0;
let startTime = new Date();
setInterval(() => {
//timer.innerText = getTime(startTime);
time++;
}, 1000);
}
function getTime(time) {
return Math.floor((new Date() - time) / 1000);
}
// function highlight() {
// if () {
// }
// }
renderQuote();
/*
Gross Words(GWPM) = All typed Words / Time taken in min
Net Words(NWPM) = Correct words / Time taken in min
Accuracy = NWPM*100/GWPM
*/
// Failed attempt to store data offline. Document.cookie uses string to store data. Not efficient for a json.
// if (document.cookie != "") {
// quotelist = document.cookie;
// } else {
// refillcookies();
// }
//------------ Other optimizations --------------