-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
95 lines (83 loc) · 2.68 KB
/
script.js
File metadata and controls
95 lines (83 loc) · 2.68 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
const typingText = document.querySelector('.typing-text p')
const input = document.querySelector('.wrapper .input-field')
const time = document.querySelector('.time span b')
const mistakes = document.querySelector('.mistake span')
const wpm = document.querySelector('.wpm span')
const cpm = document.querySelector('.cpm span')
const btn = document.querySelector('button');
//set value
let timer;
let maxTime= 60;
let timeLeft = maxTime;
let charIndex = 0;
let mistake =0;
let isTyping = false;
function loadParagraph(){
const paragraph= [ " Avoid daydreaming about the years to come.","You are the most important person in your whole life.","Always be true to who you are, and ignore what other people have to say about you.","Always be true to who you are, and ignore what other people have to say about you.","Only demonstrate your strength when it’s really required.","Subscribe to Drop X Out"
];
const randomIndex = Math.floor(Math.random()*paragraph.length);
typingText.innerHTML='';
for(const char of paragraph[randomIndex]){
console.log(char);
typingText.innerHTML+= `<span>${char}</span>`;
}
typingText.querySelectorAll('span')[0]?.classList.add('active');
document.addEventListener('keydown',()=>input.focus());
typingText.addEventListener("click",()=>{
input.focus()})
}
//Handle user input
function initTyping(){
const char= typingText.querySelectorAll('span');
const typedChar = input.value.charAt(charIndex);
if(charIndex < char.length && timeLeft >0){
if(!isTyping){
timer = setInterval(initTime,1000);
isTyping=true;
}
if(char[charIndex].innerText === typedChar){
char[charIndex].classList.add('correct');
console.log("correct");
}
else{
mistake++ ;
char[charIndex].classList.add('incorrect');
console.log("incorrect");
}
charIndex++;
char[charIndex].classList.add('active');
mistakes.innerText = mistake;
cpm.innerText = charIndex- mistake;
}
else{
clearInterval(timer);
input.value='';
}
}
function initTime(){
if(timeLeft>0){
timeLeft--;
time.innerText=timeLeft;
let wpmVal = Math.round(((charIndex - mistake)/5) /(maxTime - timeLeft)*60);
wpm.innerText = wpmVal;
}
else{
clearInterval(timer);
}
}
function reset(){
loadParagraph();
clearInterval(timer);
timeLeft = maxTime;
time.innerText= timeLeft;
input.value='';
charIndex = 0;
mistake =0;
isTyping = false;
wpm.innerText=0;
cpm.innerText=0;
mistakes.innerText=0;
}
input.addEventListener("input",initTyping);
btn.addEventListener("click",reset);
loadParagraph();