-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript1.js
More file actions
119 lines (92 loc) · 2.96 KB
/
script1.js
File metadata and controls
119 lines (92 loc) · 2.96 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
let arr=["function", "variable", "constant", "callback", "promise", "async", "await",
"react", "redux", "component", "context", "eventloop", "algorithm", "database",
"frontend", "backend", "compiler", "debugger", "runtime", "network"]
let currentLetter='';
let currentWord='';
let sec=21;
let timer=document.querySelector('.timer');
let body=document.querySelector('body');
let word=document.querySelector(".words");
let screen=document.querySelector(".screen");
let correct=0;
function getRandomWord(){
let randomIndex=Math.floor(Math.random()*arr.length);
return arr[(randomIndex)];
}
function genrateWord(){
let word=getRandomWord();
// console.log(word)
let breakWord=word.split("").map((letter)=>{
return `<span>${letter}</span>`
}).join("");
return(breakWord);
}
function putSentance(){
let spans=genrateWord();
word.insertAdjacentHTML('beforeend',`<span>${spans}</span> `)
if(currentWord===''){
currentWord=word.children[0];
// console.log(word);
currentLetter=currentWord.firstElementChild;
console.log("current",currentWord.innerText,currentLetter.innerText);
}
}
for(let i=0;i<30;i++){
putSentance();
}
body.addEventListener('keydown',(e)=>{
console.log(e.key)
if(sec===21)start();
if(e.key===currentLetter.innerText){
currentLetter.style.color="white";
currentLetter=currentLetter.nextElementSibling;
console.log(currentLetter);
correct++;
if(currentLetter===null)anotherWord();
}
else if(e.key==='Backspace'){
if(currentLetter && currentLetter.previousElementSibling){
currentLetter=currentLetter.previousElementSibling;
currentLetter.style.color="";
correct=Math.max(0,correct-1);
}
else if(currentWord && currentWord.previousElementSibling){
currentWord=currentWord.previousElementSibling;
currentLetter=currentWord.lastElementChild;
currentLetter.style.color = "";
correct = Math.max(0, correct - 1);
}
console.log("Backspace was pressed")
}
else if(e.key===' '){
if(currentLetter===null){
anotherWord();
}
console.log("Spacebar was pressed")
}
else{
currentLetter.style.color="red";
currentLetter=currentLetter.nextElementSibling;
console.log(currentLetter);
if(currentLetter===null)anotherWord();
}
})
function anotherWord(){
currentWord=currentWord.nextElementSibling;
currentLetter=currentWord.firstElementChild;
}
function start(){
sec--;
let id= setInterval(()=>{
timer.innerText=sec;
if(sec===0){
screen.style.display="block"
document.querySelector('h2').innerText=`🥳your speed is ${correct/5}🥳`
clearInterval(id);
currentLetter=null;
currentWord=null;
}
sec--;
},1000);
console.log(id);
}