-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
252 lines (219 loc) · 8.32 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
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
let content = [];
// window.addEventListener('load', getData, false )
async function getData(){
let minLength = 100;
content = [];
try{
const resp = await fetch("https://api.quotable.io/random?minLength="+minLength);
const data = await resp.json();
console.log(data);
content = [data.content];
}
catch(err){
console.log(err);
content = [["Cosmology deals with the world as the totality of space, time and all phenomena. Historically, it has had quite a broad scope, and in many cases was founded in religion. In modern use metaphysical cosmology addresses questions about the Universe which are beyond the scope of science."]];
}
finally{
console.log(content);
}
}
getData();
let para = document.querySelector("#content-to-type");
let start = document.querySelector("#start");
let minutes = document.querySelector("#minutes");
let seconds = document.querySelector("#seconds");
let keys = document.querySelectorAll(".key");
let inputContent = document.querySelector("#input-content");
inputContent.value = "";
let errorVal = document.querySelector("#error-val");
let evaluation = document.querySelector(".evaluation");
let speedVal = document.querySelector("#speed-val");
let scoreVal = document.querySelector("#score-val");
let letters,typingLetterErrors=0;
// let dataKeys = [];
// for(let i = 0;i<keys.length;i++) {
// dataKeys.push(keys.item(i).attributes[1].value);
// }
//keydown event listener
document.addEventListener("keydown",keyPress);
//updated input value event listener
inputContent.addEventListener("input",inputChange)
//start typing button event listener
start.addEventListener("click", loadContent,true);
//keypress animation
function keyPress(event){
let keyboardCode = event.keyCode;
if(keyboardCode>=48&&keyboardCode<=57) {
//inputContent.removeEventListener("input",inputChange);
// inputContent.innerHTML = inputContent.innerHTML.substr(0,inputContent.length-2)
// console.log(keyboardCode)
//inputContent.addEventListener("input",inputChange)
}
else {
keyboardCode+=32;
for(let i = 0;i<keys.length;i++) {
if(parseInt(keys.item(i).attributes[1].value)===keyboardCode){
//console.log(keys.item(i).attributes[1].value)
keys.item(i).classList.add("pressed");
setTimeout(()=>{
keys.item(i).classList.remove("pressed");
},100)
break;
}
}
}
}
function inputChange(event){
let inputVal = inputContent.value;
//.classList.remove("current");
//finding the integer equivalent of the last input character
let intVal = parseInt(inputContent.value.charAt(inputContent.value.length-1));
//if integer value is not NaN i.e. it is a number, remove it from the input value
if(!Number.isNaN(intVal)) inputContent.value = inputVal.substr(0,inputContent.value.length-1);
// console.log(inputContent.value)
let keyCode = inputVal.charCodeAt(inputVal.length-1);
if(inputVal.length<=para.length-1)
startBlinking(inputVal.length)
checkInput(keyCode,inputVal);
}
function startBlinking(letterId){
window.blinkingId = setInterval(()=>{
blinking(letters.item(letterId));
},150);
}
function clearBlinking(){
clearInterval(window.blinkingId);
}
function blinking(letter){
if(letter!=null && letter.classList[1]==="current")
letter.classList.remove("current");
else {
letter.classList.add("current");
}
}
//letters.item(inputValue.length-1).innerHTML.charAt(0)==="_"&& inputValue ||
function checkInput(keyCode,inputValue){
if(inputValue.length===letters.length){
pauseClock("Pause");
let [minsCompleted,secsCompleted] = getCurrentTimerValue(minutes.innerHTML,seconds.innerHTML);
let totalCorrect = letters.length - typingLetterErrors;
let percentageCorrect = Math.floor(totalCorrect/letters.length * 100);
let charsPerSecond = Math.floor(totalCorrect/(minsCompleted*60 + secsCompleted));
let wordsPerMinute = Math.floor(charsPerSecond*60)/10;
speedVal.innerHTML = wordsPerMinute+" words/min";
scoreVal.innerHTML = percentageCorrect;
start.innerHTML = "Restart";
//start.removeEventListener("click",loadContent,true)
console.log("Percentage =" +percentageCorrect)
console.log("Speed "+charsPerMinute+ " per minute")
}
else if(inputValue.length<letters.length){
if(keyCode>=97 && keyCode<=122 || keyCode===32){
if(keyCode===32) keyCode = 95;
if(letters.item(inputValue.length-1).innerHTML.charCodeAt(0)===keyCode){
//console.log(letters.item(inputValue.length-1),inputValue)
clearBlinking();
startBlinking(inputValue.length);
letters.item(inputValue.length-1).classList.add("typed");
}
else {
typingLetterErrors++;
errorVal.innerHTML = typingLetterErrors;
clearBlinking();
startBlinking(inputValue.length);
letters.item(inputValue.length-1).classList.add("wrong");
console.log("Errors = "+typingLetterErrors)
}
}
}
}
//event.key.charCodeAt(0);
//console.log(inputVal.charCodeAt(inputVal.length-1))
//checkCurrentInput(event.key,para.innerHTML.charAt(0)
function loadContent(){
if(start.innerHTML==="Restart"){
window.location.reload();
}
inputContent.focus();
if(start.innerHTML==="Start"){
setNewContent();
para = document.querySelector("#content-to-type");
}
else if (start.innerHTML==="Resume"){
startClock(seconds.innerHTML,minutes.innerHTML,start.innerHTML);//resume timer
start.innerHTML = "Pause";
}
else if (start.innerHTML === "Pause"){
pauseClock(start.innerHTML); //pausing the clock
start.innerHTML="Resume";
}
}
function setNewContent(){
typingLetterErrors=0;
let paraNumber = randomParaGenerator(1);
let wordsArray = wordSeparator(content[0].toLocaleLowerCase());
let typingPara =" ";
wordsArray = wordsArray.filter((word)=> word.length>=1)
//limiting to 25 words
let letterId = 0;
for(let wordIndex = 0;wordIndex<Math.min(25,wordsArray.length);wordIndex++){
let word = wordsArray[wordIndex];
console.log("word ",wordIndex,word)
typingPara = typingPara+letterSeparator(word,letterId);
letterId = letterId+word.length+1;
}
para.innerHTML = typingPara.trim();
letters = document.querySelectorAll(".typing");
startClock(seconds.innerHTML,minutes.innerHTML,start.innerHTML);//start timer
start.innerHTML = "Pause";
}
function wordSeparator(currContent){
return currContent.split(/[,. -_]/)
}
function letterSeparator(currContent,id){
let wordSpan="";
for(let charIndex=0;charIndex<currContent.length;charIndex++){
wordSpan= wordSpan + `<div class="typing" id="${id}">${currContent.charAt(charIndex)}</div>`;
id++;
}
wordSpan.trim();
wordSpan= wordSpan + `<div class="typing" id="${id}">_</div>`
return wordSpan;
}
function randomParaGenerator(limit){
return Math.floor(Math.random()*limit);
}
function startClock(secs,mins,condition){
let timerVals = getCurrentTimerValue(mins,secs);
let [minsVal,secsVal] = [...timerVals];
//After every second, check the value of condition
//If it is start, call the increase seconds function
//IF not, do nothing
startBlinking(inputContent.value.length)
//using window object because using local variables result in error in pausing the clock because of scoping
window.timerClockID = setInterval(() => {
if(condition==="Start"|| condition==="Resume"){
secsVal = increaseSeconds(secsVal);
if(secsVal>59){
secsVal = 0;
minsVal=minsVal+1;
}
seconds.innerHTML = secsVal;
minutes.innerHTML = minsVal;
}
}, 1000);
}
function getCurrentTimerValue(mins,secs){
let secsVal = parseInt(secs);
let minsVal = parseInt(mins);
let timer = [minsVal,secsVal];
return timer;
}
function increaseSeconds(secondsValue){
return secondsValue+1;
}
function pauseClock(condition){
if(condition==="Pause"){
clearInterval(window.timerClockID)
}
}