-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
87 lines (64 loc) · 2.09 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
let msg = document.getElementById('msg');
let textarea = document.getElementById('mykeywords');
let btn = document.getElementById('btn');
let starttime, endtime;
const onplay = async () => {
// let num = Math.floor(Math.random() * datas.length);
const api = "https://type.fit/api/quotes";
try {
msg.innerText = "just a second...";
let data = await fetch(api);
const mydata = await data.json();
let num = Math.floor(Math.random() * mydata.length);
msg.innerText = mydata[num].text;
let date = new Date();
starttime = date.getTime();
// console.log(starttime);
btn.innerHTML = 'Done';
} catch (error) {
msg.innerHTML = `server mistake | -> retry`
}
}
const offplay = () => {
let date = new Date();
endtime = date.getTime();
let totaltime = ((endtime - starttime) / 1000);
let lengthofwords = calculate(textarea.value);
let speed = Math.round((lengthofwords / totaltime) * 60);
let result = ` speed is ${speed} words per minute / `
result += calculatewords(msg.innerHTML, textarea.value);
if (textarea.value == "") {
msg.innerHTML = "why are you not writing text in the textarea";
} else {
msg.innerHTML = result;
}
}
const calculatewords = (str1,str2) => {
let words1 = str1.split(" ");
let words2 = str2.split(" ");
let count = 0;
words1.forEach((item, index) => {
if (item == words2[index]) {
count++;
}
})
let wrongwords = (words1.length - count);
return ` worng word is ${wrongwords} / currect words is ${count} `;
}
const calculate = (str) => {
let sendwordslength = str.split(" ").length;
return sendwordslength;
}
btn.addEventListener('click', function () {
if (this.innerHTML == "Start") {
textarea.disabled = false;
onplay();
if (textarea.value != "") {
textarea.value = "";
}
} else if (this.innerHTML == "Done") {
this.innerHTML ="Start";
textarea.disabled = true;
offplay();
}
})