-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path09_javascript.js
More file actions
133 lines (119 loc) · 4.04 KB
/
09_javascript.js
File metadata and controls
133 lines (119 loc) · 4.04 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
const quoteApiUrl = "https://api.freeapi.app/api/v1/public/quotes/quote/random";
const quoteSection = document.getElementById("quote");
const userInput = document.getElementById("quote-input");
let quote = "";
let time = 60;
let timer = "";
let mistakes = 0;
// Display random quotes
const renderNewQuote = async () => {
const response = await fetch(quoteApiUrl);
let data = await response.json();
// console.log(data);
quote = data.data.content;
// console.log(quote);
const minLength = 100;
const maxLength = 160;
if (quote.length > maxLength) {
// Trim the quote to maxLength and avoid cutting in the middle of a word
quote = quote.substring(0, maxLength);
let lastSpace = quote.lastIndexOf(" ");
quote = quote.substring(0, lastSpace) + "...";
} else if (quote.length < minLength) {
// Fetch a new quote if it's too short
return renderNewQuote();
}
// array of characters in the quote
let arr = quote.split("").map((value) => {
// wrap characters in span tag
return "<span class='quote-chars'>"+ value +"</span>";
})
// join array for displaying
quoteSection.innerHTML = arr.join("");
// console.log(arr);
}
window.onload = () => {
userInput.value = "";
document.getElementById("start-test").style.display = "block";
document.getElementById("stop-test").style.display = "none";
userInput.disabled = true;
renderNewQuote();
};
// logic for comparing input and quote
userInput.addEventListener("input", () => {
let quoteChars = document.querySelectorAll(".quote-chars");
// console.log(quoteChars);
// from quoteChars create an array
quoteChars = Array.from(quoteChars);
// array of user input
let userInputChars = userInput.value.split("");
quoteChars.forEach((char, index) => {
// check if quote = userinput
if(char.innerText == userInputChars[index]){
char.classList.add("success");
}
else if(userInputChars[index] == null){
if(char.classList.contains("success")){
char.classList.remove("success");
}
else{
char.classList.remove("fail");
}
}
// not same
else{
if(!char.classList.contains("fail")){
mistakes += 1;
char.classList.add("fail");
}
document.getElementById("mistakes").innerText = mistakes;
}
// return true if all characters are entered
let check = quoteChars.every((element) => {
return element.classList.contains("success");
});
// end if all characters are correct
if(check){
// console.log("working");
displayResult();
}
})
})
// Start test
const startTest = () => {
mistakes = 0;
timer = "";
userInput.disabled = false;
reduceTime();
document.getElementById("start-test").style.display = "none";
document.getElementById("stop-test").style.display = "block";
}
// update timer
function updateTimer(){
if(time == 0){
// end
displayResult();
}
else{
document.getElementById("timer").innerText = --time + "sec.";
}
}
// reduce time
const reduceTime = () => {
time = 60;
timer = setInterval(updateTimer, 1000);
}
// end test
const displayResult = () => {
// display result div
document.querySelector(".result").style.display = "block";
clearInterval(timer);
document.getElementById("stop-test").style.display = "none";
userInput.disabled = true;
let timeTaken = (60 - time) / 100;
if (timeTaken <= 0){
timeTaken = 1;
}
document.getElementById("wpm").innerText = (userInput.value.length / 5 / timeTaken).toFixed(2)+"wpm";
document.getElementById("accuracy").innerText = Math.round(((userInput.value.length - mistakes) / userInput.value.length)* 100) + "%";
};