-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
322 lines (251 loc) · 9.38 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
// Welcome screen - Provide Necessary Info
const readyBtn=document.querySelector("#ready-btn");
const welcomeScreen=document.querySelector(".welcome-section");
const handleReadyBtnClick=()=>{
// alert("Typing Speed Test Started");
welcomeScreen.classList.add("welcome-section-close");
welcomeScreen.classList.remove("welcome-section");
}
readyBtn.addEventListener("click",handleReadyBtnClick);
// Main screen - Typing test interface
// accessing elements on main screen
let wpm=document.querySelector("#wpm");
let cpm=document.querySelector("#cpm");
let errors=document.querySelector("#errors");
let time=document.querySelector("#time");
let accuracy=document.querySelector("#accuracy");
let textToTypeContainer=document.querySelector("#text-to-type-container");
let textTypingSection=document.querySelector("#text-of-typing-section");
let startBtn=document.querySelector("#start-btn");
let stopBtn=document.querySelector("#stop-btn");
let resultSection =document.querySelector("#result-section");
// text for typing
const textForTyping =[
[ "The sun is shining brightly today.",
"I love to eat pizza with extra cheese.",
"My cat likes to sleep on the soft rug.",
"The red car drove slowly down the road.",
"We went for a walk in the park yesterday."
],
[
"The quick brown fox jumps over the lazy dog.",
"Pack my box with five dozen liquor jugs.",
"How razorback-jumping frogs can level six piqued gymnasts!",
"Jinxed wizards pluck ivy from the big quilt.",
"The five boxing wizards jump quickly."
],
[
"Scientists have discovered a new species of deep-sea fish.",
"The intricacies of quantum mechanics baffle even the brightest minds.",
"Philosophers debate the nature of existence and the meaning of life.",
"Literature offers a window into the human condition and our collective experiences.",
"Artists express their emotions and perspectives through their creative works."
]
]
// Declaration of variable used
var stopBtnClicked=0;
var textProvided = "";
var charProvided = "";
var typedText ="";
var typedChar="";
var errorCount=0;
var charCount=0;
var wordCount=1;
var timeTakenForTyping=0;
// provide text as typing test start
const textProvider=(first,second)=>{
textProvided = textToTypeContainer.innerText=textForTyping[first][second];
}
// provide time as typing test start
const timeProvider=()=>{
let timeProvided=60;
time.innerText=timeProvided;
const timing=()=>{
if(timeProvided!==0){
if(stopBtnClicked>=1){
clearInterval(givenInterval);
timeTakenForTyping=60-timeProvided;
setTimeout(()=>{
resultSection.style.display="flex";
},2000);
}
else{
timeProvided--;
time.innerText=timeProvided;
timeTakenForTyping=60-timeProvided;
}
}
else{
clearInterval(givenInterval);
textTypingSection.setAttribute("disabled", "disabled");
textTypingSection.style.backgroundColor="red";
textTypingSection.placeholder="Time Out";
timeTakenForTyping=60-timeProvided;
setTimeout(()=>{
resultSection.style.display="flex";
},2000);
}
}
var givenInterval= setInterval(timing,1000);
}
// This event listener prevent user to press backspace and type more chars then provided
textTypingSection.addEventListener("keydown", (e) => {
const { key } = e;
const isBackspace = key === "Backspace";
const isMaxLengthReached = textTypingSection.value.length >= textProvided.length;
if (isBackspace || isMaxLengthReached) {
e.preventDefault();
}
});
// Checking and matching typed chars and words.
const checkTyping=()=>{ // checking current input and comparing from provided
textTypingSection.addEventListener("input",()=>{
typedText=textTypingSection.value;
let currentIndex=typedText.length;
typedChar=typedText[currentIndex-1];
charProvided= textProvided[currentIndex-1]
if(typedChar===charProvided){
if(typedChar===" "){
wordCount++;
}
charCount++;
textTypingSection.classList.remove("incorrect-char");
textTypingSection.classList.add("correct-char");
}
else{
// charCount++; // In case of wrong char entered no increment in char count bcz its creates final performance.
errorCount++;
textTypingSection.classList.remove("correct-char");
textTypingSection.classList.add("incorrect-char");
}
statistics();
})
}
// Creating statistics and displaying
const statistics =()=>{
errors.innerText=errorCount;
let accurateTypedChar= charCount-errorCount;
let accuracyCal=(accurateTypedChar*100)/charCount;
accuracy.innerText=accuracyCal.toFixed(0);
cpm.innerText=charCount;
wpm.innerText=wordCount;
performanceDisplay(charCount,wordCount);
}
// generating random number
const getRandomInt=(min,max)=>{
min=Math.ceil(min);
max=Math.floor(max);
return Math.floor(Math.random()*(max-min+1))+min;
}
// Handle typing test start event
const startTypingTest=()=>{
let firstNo=getRandomInt(0,textForTyping.length-1);
let secondNo=getRandomInt(0,textForTyping[firstNo].length-1);
textProvider(firstNo,secondNo);
timeProvider();
checkTyping();
}
const handleStopBtnClick=()=>{
stopBtnClicked++;
textTypingSection.setAttribute("disabled", "disabled");
textTypingSection.style.backgroundColor="red";
textTypingSection.placeholder="Stopped";
if(textTypingSection.value==""){
performanceDisplay(charCount,wordCount);
}
}
// Control Section
startBtn.addEventListener("click",()=>{
textTypingSection.removeAttribute("disabled");
textTypingSection.placeholder ="Star typing here ....";
startTypingTest();
startBtn.classList.add("remove-start-btn");
stopBtn.style.display="inline";
performanceDisplay();
})
stopBtn.addEventListener("click",handleStopBtnClick);
// Result Section
// Result Section-1
const contactMeBtn =document.querySelector("#contact-me-btn");
const stars = document.querySelectorAll(".star");
const ratingMsg = document.querySelector("#rating-msg");
const messages = [
"Could be better ⭐😞",
"Okay ⭐⭐😐",
"Good ⭐⭐⭐🙂",
"Great ⭐⭐⭐⭐😃",
"Amazing ⭐⭐⭐⭐⭐😍"
];
// Handle Contact Me btn clicked event
contactMeBtn.addEventListener("click",()=>{
window.open("https://github.com/dev-kant-kumar", "_blank");
})
stars.forEach((star, index) => {
star.addEventListener("mouseover", () => {
highlightStars(index);
ratingMsg.innerText = messages[index];
});
star.addEventListener("click", () => {
setRating(index);
});
});
function highlightStars(index) {
stars.forEach((star, i) => {
if (i <= index) {
star.classList.add("star-hover");
} else {
star.classList.remove("star-hover");
}
});
}
function setRating(index) {
stars.forEach((star, i) => {
if (i <= index) {
star.classList.add("star-hover");
} else {
star.classList.remove("star-hover");
}
});
ratingMsg.innerText = messages[index];
}
const timeTaken = document.querySelector("#time-taken");
const wordsLeft = document.querySelector("#words-left");
const charsLeft = document.querySelector("#chars-left");
const msgForUser =document.querySelector("#msg-user");
const restartBtn =document.querySelector("#restart-btn");
const performanceDisplay=(CharCountResult,wordCountResult)=>{
const wordsInTextProvided=textProvided.split(" ").length;
const charsInTextProvided = textProvided.length;
if(CharCountResult==1){
timeTaken.innerText="60s";
charsLeft.innerText=charsInTextProvided;
wordsLeft.innerText=wordsInTextProvided;
}
else{
timeTaken.innerText=timeTakenForTyping;
charsLeft.innerText=charsInTextProvided - CharCountResult;
wordsLeft.innerText=wordsInTextProvided - wordCountResult ;
}
performanceMsg(CharCountResult);
}
const performanceMsg=(charTyped)=>{
if(charTyped==0){
msgForUser.innerText="You have not typed anything!";
}
else if(charTyped==textProvided.length){
msgForUser.innerText="Incredible job! Your typing speed and accuracy are outstanding!";
}
else if(charTyped>=textProvided.length-5){
msgForUser.innerText="Your typing speed is good, keep practicing and you'll see great progress!";
}
else if(charTyped>textProvided.length || charTyped <0){
msgForUser.innerText="You exceed on chars.";
}
else{
msgForUser.innerText="Practice more. You'll get better over time.";
}
}
const handleRestartBtnClick=()=>{
alert("Restarting......");
}
restartBtn.addEventListener("click",handleRestartBtnClick);