-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.js
77 lines (72 loc) · 2 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
let time = 1 * 60;
let charIndex = 0;
let mistakes = 0;
let isTyping = false;
let cpm = 0;
let wpm = 0;
// Fetching paragraphs
let paraArea = document.querySelector(".paraArea");
function loadPara() {
const randPara = Math.floor(Math.random() * paragraphs.length);
paraArea.innerHTML = "";
paragraphs[randPara].split("").forEach((char) => {
let spans = `<span>${char}</span>`;
paraArea.innerHTML += spans;
});
document.addEventListener("keydown", () => input.focus() );
}
// Typing function
function typing() {
let characters = document.querySelectorAll("span");
let input = document.getElementById("input-feild");
let typedChar = input.value.split("")[charIndex];
if (time > 0) {
if (!isTyping) {
timer = setInterval(timer, 1000);
isTyping = true;
}
if (typedChar == null) {
charIndex--;
if (characters[charIndex].classList.contains("incorrect")) {
mistakes--;
}
if (characters[charIndex].classList.contains("correct")) {
cpm--;
}
characters[charIndex].classList.remove("correct", "incorrect");
} else {
if (typedChar == characters[charIndex].innerText) {
characters[charIndex].classList.add("correct");
cpm++;
} else {
characters[charIndex].classList.add("incorrect");
mistakes++;
}
charIndex++;
}
document.getElementById("mistake").innerHTML = mistakes;
document.getElementById("cpm").innerHTML = cpm;
} else {
clearInterval(timer);
input.innerHTML = "";
}
}
// Timer
function timer() {
if (time > 0) {
time--;
document.getElementById("timer").innerHTML = time;
console.log(cpm);
let wpm = Math.round(cpm / 5);
document.getElementById("wpm").innerHTML = wpm;
} else {
// console.log("samay samapt");
clearInterval(timer);
}
}
loadPara();
let input = document.getElementById("input-feild");
input.addEventListener("input", typing);
document.getElementById('btn').addEventListener('click', ()=>{
location.reload();
})