-
Notifications
You must be signed in to change notification settings - Fork 1
/
typeit.js
214 lines (181 loc) · 5.81 KB
/
typeit.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
const RANDOM_TEXT_URL = 'https://api.quotable.io/random?minLength=70' // Api for Fetching Data
let query = new URLSearchParams(window.location.search)
let time = query.get('time')
const paragraph_display_element = document.getElementById('paragraph')
const user_text_element = document.getElementById('user_text')
const timer_element = document.getElementById('timer')
const wpm_text = document.getElementById("wpm")
const accuracy_text = document.getElementById("accuracy")
let user_typed_char = []
user_text_element.addEventListener("input", ()=> {
const array_of_para = paragraph_display_element.querySelectorAll("span")
const array_of_usertext = user_text_element.value.split("")
//console.log(array_of_usertext)
user_typed_char = array_of_usertext
let correct = true
array_of_para.forEach((character_span, index) => {
const character = array_of_usertext[index]
//color coding mistakes and correct letters
if( character == null ) {
character_span.classList.remove("correct")
character_span.classList.remove("incorrect")
correct = false
}
else if(character == character_span.innerText) {
correct_sound.play()
character_span.classList.add("correct")
character_span.classList.remove("incorrect")
}
else {
wrong_sound.play()
character_span.classList.remove("correct")
character_span.classList.add("incorrect")
correct = false
}
})
if(correct) {
render_new_text() // After finishing
}
})
// Fetching random data Fn
function get_random_text() {
return fetch(RANDOM_TEXT_URL)
.then((res)=>res.json())
.then(data => data.content)
/* .then(function(data){
console.log(typeof(data.quote))
}) */
}
function time_converter (){
let temp = time/30
return temp
}
let api_text = []
async function render_new_text() {
let text = ""
let minutes = time_converter()
let accumulator = minutes
for(let i=0;i<accumulator;i++){
text = text + await get_random_text() + " "
}
console.log(text)
api_text = text.split("")
paragraph_display_element.innerHTML = ""
text.split("").forEach(character => {
const character_span = document.createElement("span")
character_span.innerText = character
paragraph_display_element.appendChild(character_span)
})
user_text_element.value = null
start_timer()
}
function wpm_timer(){
let temp = setInterval(()=>{
if(count==1){
clearInterval(temp)
}
update_wpm()
update_accuracy()
},1000)
}
let wpm_counter = 0 //
let wpm = [] //
let times = [] //
let wpm_data = {wpm: wpm, time: times} //
function update_wpm(){
let current_time = timer_element.innerHTML
current_time = current_time.split(" ")[2]
let elapsed_time = time-current_time
let text = user_text_element.value
let words = text.length/9
let temp = Math.floor((120*words)/elapsed_time)
if(elapsed_time!==30 && elapsed_time%5==0){ //
wpm.push(temp) //
times.push(elapsed_time) //
localStorage.setItem("wpm_data", JSON.stringify(wpm_data)) //
}
if(wpm_counter==0 || temp==NaN){
console.log("hello")
wpm_text.innerHTML = "WPM : 0"
}
else{
wpm_text.innerHTML = "WPM : "+temp
}
wpm_counter++
}
let start_time;
let count = time
function start_timer() {
timer_element.innerText = null
let temp = setInterval(() => {
if(count==1){
clearInterval(temp)
after_time_out()
}
timer.innerText = `Timer : ${get_timer_time(count)}`
}, 1000)
}
function after_time_out(){
let final_accuracy = accuracy_text.innerHTML
final_accuracy = final_accuracy.split(" ")[2].split("%")[0]
let final_wpm = wpm_text.innerHTML
let query = new URLSearchParams(window.location.search)
query.set('accuracy',final_accuracy)
query.set('wpm',final_wpm)
let pathname = `result.html`;
window.location.href = `${pathname}?${query.toString()}`;
}
function get_timer_time(time) {
count = count-1
return time-1
}
render_new_text()
wpm_timer()
let acc_counter = 0
function update_accuracy(){
let error = 0
let accuracy = 0
let temp1=[]
if(user_typed_char.length==0 || acc_counter==0){
accuracy_text.innerHTML = "Accuracy : 0%"
}
else{
for(let i=0; i<user_typed_char.length; i++){
if(user_typed_char[i]!==api_text[i]) {
temp1.push(user_typed_char[i])
}
error = Math.round((temp1.length/user_typed_char.length)*100)
accuracy = 100 - error
accuracy_text.innerHTML = "Accuracy : "+accuracy+"%";
}
}
acc_counter++
}
function sound(src) {
this.sound = document.createElement("audio");
this.sound.src = src;
this.sound.setAttribute("preload", "auto");
this.sound.setAttribute("controls", "none");
this.sound.style.display = "none";
document.body.appendChild(this.sound);
this.play = function(){
this.sound.play();
}
this.stop = function(){
this.sound.pause();
}
}
var correct_sound;
var wrong_sound;
function correct_key() {
correct_sound = new sound("click.mp3");
}
function wrong_key(){
wrong_sound = new sound("error.mp3");
}
correct_key()
wrong_key()
let restart = document.getElementById("restart")
restart.addEventListener("click",function(){
window.location.href = `typeit.html?time=${time}`
})