-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
116 lines (106 loc) · 3.54 KB
/
index.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
const paragraph = document.querySelector(".fill-out");
let inputValue = document.querySelector("input");
let mistakes = document.querySelector(".right-mistakes");
let time = document.querySelector(".right-time");
let cpm = document.querySelector(".right-cpm");
let wpm = document.querySelector(".right-wpm");
let reset = document.querySelector(".try-again");
let count = 0;
let count1 = 0;
let count2 = 60;
let maxTime = 60;
let isTrue = true;
let intervalId;
const randomParagraphGenerator = () => {
randomIndex = Math.floor(Math.random() * paragraphs.length);
paragraphs[randomIndex].split("").forEach((cur_val) => {
let newElement = document.createElement("span");
newElement.innerHTML = cur_val;
paragraph.append(newElement);
});
};
const inputFocused = () => {
document.querySelector("input").focus();
};
document.addEventListener("keydown", (e) => {
inputFocused();
});
document.querySelector(".padding").addEventListener("click", (e) => {
inputFocused();
});
inputValue.addEventListener("input", (e) => {
const allCharacters = document.querySelectorAll("span");
let typedCharacter = inputValue.value;
if (count < allCharacters.length && count2 > 0) {
if (isTrue) {
intervalId = setInterval(() => {
if (count2 > 0) {
time.innerHTML = `${--count2}s`;
} else {
clearInterval(intervalId);
}
}, 1000);
isTrue = false;
}
if (typedCharacter[count] == null) { //when the inputfield is empty, it will not gonna execute as typed character doesnt exist
--count;
allCharacters[count].style.color = "black";
allCharacters[count].style.fontWeight = "normal";
allCharacters[count].style.backgroundColor = "white";
allCharacters[count].style.padding = "0px";
console.log(count);
if (allCharacters[count].classList.contains("incorrect")) {
mistakes.innerHTML = --count1;
allCharacters[count].classList.remove("incorrect");
}
} else {
if (allCharacters[count].innerHTML == typedCharacter[count]) {
// console.log("correct");
allCharacters[count].style.color = "white";
allCharacters[count].style.fontWeight = "bolder";
allCharacters[count].style.backgroundColor = "green";
allCharacters[count].style.padding = "2px";
} else {
allCharacters[count].style.color = "white";
allCharacters[count].style.fontWeight = "bolder";
allCharacters[count].style.backgroundColor = "red";
allCharacters[count].style.padding = "2px";
allCharacters[count].classList.add("incorrect");
mistakes.innerHTML = ++count1;
}
count++;
}
allCharacters.forEach((cur_val) => {
cur_val.classList.remove("active");
});
allCharacters[count].classList.add("active");
cpm.innerHTML = count - count1;
let save = Math.round(((count - count1) / 5 / (maxTime - count2)) * 60);
if (save < 0 || !save || save == Infinity || save == NaN) {
wpm.innerHTML = 0;
} else {
wpm.innerHTML = save;
}
} else {
inputValue.value = "";
clearInterval(intervalId);
}
});
reset.addEventListener("click", (e) => {
paragraph.innerHTML="";
randomParagraphGenerator();
document.querySelector("span").classList.add("active");
inputValue.value = "";
count = 0;
count1 = 0;
count2 = 60;
maxTime = 60;
isTrue = true;
mistakes.innerHTML=0;
wpm.innerHTML=0;
cpm.innerHTML=0;
time.innerHTML=`${60}s`;
clearInterval(intervalId);
});
randomParagraphGenerator();
document.querySelector("span").classList.add("active");