-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
187 lines (174 loc) · 5.65 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
body {
background-color: #646669;
}
.content {
display: flex;
flex-direction: column;
margin-top: 20px;
justify-content: center;
align-items: center;
}
.blur {
color: transparent;
text-shadow: 0 0 20px #000;
}
.heading {
color: rgb(214, 199, 199);
}
.text {
width: 40rem;
height: 20rem;
background-color: #646669;
padding: 20px;
text-align: left;
font-size: 2rem;
padding-top: 40px;
font: bold;
color: #939ba7;
}
.result {
text-align: left;
font-size: 2rem;
padding-top: 40px;
font: bold;
color:white;
}
.restart {
border-color: white;
color: #c4cbd5;
background-color: #646669;
margin-top: 1rem;
width: 6rem;
height: 3rem;
border-radius: 25px;
}
.current{
border-left: 1px solid black;
}
</style>
</head>
<body>
<h1 class="heading">Typing Test</h1>
<div class="content">
<h1 class="time">
60 s
</h1>
<div class="text">
</div>
<div class="result">
</div>
<button class="restart">
Restart
</button>
</div>
<script>
let url = "https://api.quotable.io/random";
let text = document.getElementsByClassName("text")[0];
let r = document.getElementsByClassName("restart")[0];
let time = document.getElementsByClassName("time")[0];
let span = document.getElementsByTagName("span");
let total_characters = 0;
let error_character = 0;
let myInterval;
let index = 0;
let audio=new Audio("click.wav");
let audio1=new Audio("error.mp3");
let flag = true;
let t = 60;
async function fetch_api() {
let response = await fetch(url);
let data = await response.json();
let a = data.content;
text.innerHTML = "";
let s;
for (let i = 0; i < a.length; i++) {
s = document.createElement("span");
s.innerHTML = a[i];
text.appendChild(s);
}
}
function keypressed(event) {
console.log(event);
if (flag) {
myInterval = setInterval(timer, 1000);
flag = false;
}
if (event.key!="Shift" && event.key!="CapsLock") {
span[index].classList.remove("current");
console.log(event.key);
if (event.key == "Backspace") {
if (index != 0) {
index--;
if (span[index].style.color == "red") {
error_character--;
}
else {
total_characters--;
}
if (span[index].innerHTML == " ") {
span[index].style.backgroundColor = "#646669";
}
else {
span[index].style.color = "#939ba7";
}
}
}
else if (event.key == span[index].innerHTML) {
span[index].style.color = "white";
index++;
total_characters++;
audio.play();
}
else {
if (span[index].innerHTML == " ") {
span[index].style.backgroundColor = "red";
}
span[index].style.color = "red";
index++;
error_character++;
audio1.play();
}
if (index == span.length) {
index = 0;
fetch_api();
}
if(index!=0){
span[index].classList.add("current");
}
}
}
function timer() {
time.innerHTML = t + " s";
t--;
if (t == -1) {
t = 60;
display_result();
}
}
function display_result() {
clearInterval(myInterval);
let result = document.getElementsByClassName("result")[0];
result.innerHTML = "";
const words = total_characters / 5;
let speed = Math.round((words - error_character));
let error_per = (error_character) / ((total_characters+error_character) * 100);
result.textContent = "Speed :" + speed + "WPM\r\n Accuracy:" + Math.floor(100 - error_per).toFixed(1) + "%";
document.removeEventListener("keydown", keypressed);
}
function restart() {
location.reload();
}
fetch_api();
document.addEventListener("keydown", keypressed);
r.addEventListener("click", restart);
</script>
</body>
</html>