Skip to content

Commit 50a69c3

Browse files
committed
typing speed test best score
1 parent 5e1ca00 commit 50a69c3

2 files changed

Lines changed: 77 additions & 23 deletions

File tree

js/typo.js

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,25 @@ const letter = document.getElementById('letter');
44
const reset = document.getElementById('reset');
55
const result = document.getElementById('results');
66
const info = document.getElementById('info');
7+
const HS = document.getElementById('best');
78
const cb = document.getElementById('shuffle');
89
let index = 0;
910
letter.innerHTML = arr[index];
1011
const response = [];
12+
let currentBestReg = Number(localStorage.getItem('HSR'));
13+
let currentBestSuf = Number(localStorage.getItem('HSS'));
14+
currentBestReg = Number.isNaN(currentBestReg) ? 0 : currentBestReg;
15+
currentBestSuf = Number.isNaN(currentBestSuf) ? 0 : currentBestSuf;
16+
if(currentBestReg > 0) {
17+
HS.innerHTML = `Best time ${currentBestReg}s`;
18+
}
1119

1220
typer.addEventListener('input', (event) => {
1321
try {
14-
if (!!event.inputType.match('insert')) {
22+
if(event.inputType.toUpperCase().match(/PASTE|DROP/g)) {
23+
resetAction();
24+
throw Error('<font color="red">Pasting/Dropping text is not fair 😞. We are resetting the game.</font >');
25+
} else if (!!event.inputType.match('insert')) {
1526
if (event.data[event.data.length - 1].toUpperCase() === arr[index]) {
1627
index++;
1728
response.push(Date.now());
@@ -20,11 +31,19 @@ typer.addEventListener('input', (event) => {
2031
if (index < 26) {
2132
letter.innerHTML = arr[index];
2233
} else {
23-
letter.innerHTML = `Your time: ${(response[25] - response[0]) / 1000}s`;
34+
const log = (response[25] - response[0]) / 1000;
35+
letter.innerHTML = `Your time: ${log}s`;
2436
typer.setAttribute('disabled', 'true');
25-
result.innerHTML = `<div class="title">Final Results 🏆</div> ${getMeResults(response)}`;
37+
result.innerHTML = `<div class="title w-100">Final Results 🏆</div> ${getMeResults(response)}`;
2638
index = 0;
2739
info.innerHTML = '';
40+
if( (cb.checked && (log < currentBestSuf || currentBestSuf == 0)) ||
41+
(!cb.checked && (log < currentBestReg || currentBestReg == 0))) {
42+
localStorage.setItem(cb.checked ? 'HSS' : 'HSR',log);
43+
currentBestSuf = cb.checked ? log : currentBestSuf;
44+
currentBestReg = cb.checked ? currentBestReg: log;
45+
HS.innerHTML = cb.checked ? `Shuffled Best time ${log}s` :`Best time ${log}s`;
46+
}
2847
}
2948
}
3049

@@ -47,8 +66,10 @@ function resetAction() {
4766
index = 0;
4867
if (cb.checked) {
4968
shuffle(arr);
69+
HS.innerHTML = currentBestSuf>0 ? `Shuffled Best time ${currentBestSuf}s` : '';
5070
} else {
5171
arr.sort();
72+
HS.innerHTML = currentBestReg>0 ? `Best time ${currentBestReg}s` : '';
5273
}
5374
typer.value = '';
5475
letter.innerHTML = arr[index];
@@ -62,7 +83,6 @@ function resetAction() {
6283
function getMeResults(response) {
6384
const start = response[0];
6485
let result = '';
65-
let index =
6686
response.forEach((element, ind) => {
6787
result += `<div class="element"><div class="key">${arr[ind]}</div> <div class="time"> ${(element - start === 0) ? 'Start' : (element - start) / 1000}s </div></div>`
6888
});

typing-test.html

Lines changed: 53 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@
77
<meta name="viewport" content="width=device-width, initial-scale=1.0">
88
<title>Type Fast</title>
99
<style>
10+
html {
11+
font-family: Verdana, Geneva, Tahoma, sans-serif;
12+
}
13+
14+
.w-100 {
15+
width: 100% !important;
16+
}
17+
1018
.results {
1119
display: flex;
1220
align-items: center;
@@ -20,12 +28,11 @@
2028

2129

2230
input[type="reset"i] {
23-
padding: 5px 15px;
24-
font-size: 24px;
31+
padding: 10px 15px;
32+
font-size: 16px;
2533
background-color: rgba(6, 8, 17, 0.822);
2634
color: white;
2735
border: none;
28-
margin: 1px;
2936
border-radius: 5px;
3037
}
3138

@@ -40,7 +47,7 @@
4047
box-shadow: 0 5px 5px #b6b6b6;
4148
text-shadow: 0 0.5px 1px #777, 0 2px 6px #f2f2f2;
4249
}
43-
50+
4451
.key:hover {
4552
box-shadow: none;
4653
animation: name 1s ease 0s 1 alternate forwards;
@@ -57,9 +64,11 @@
5764
text-align: center;
5865
font-size: 24px;
5966
padding: 20px;
60-
background-color: #dadada;
67+
background-color: rgba(255, 255, 255, .2);
6168
border-radius: 20px;
6269
width: 350px;
70+
box-shadow: inset 0 0 9px 0px #b6b6b6;
71+
opacity: 0.6;
6372
}
6473

6574
.element {
@@ -77,60 +86,85 @@
7786
font-size: 24px;
7887
outline: none;
7988
width: 300px;
80-
margin-right: 1px;
89+
font: 100 14px sans;
90+
border: 0;
91+
}
92+
93+
.display-none {
94+
display: none;
8195
}
8296

8397
.reset {
8498
margin-left: auto !important;
85-
margin-top: 1px;
86-
margin-bottom: 1px;
8799
}
88100

89101
.input-area {
90102
display: flex;
91103
justify-content: center;
92104
flex-wrap: wrap;
93105
margin: 10px 20px;
94-
color: darkslategrey;
95-
font-size: 32px;
106+
font-size: 28px;
107+
border-radius: 0 7px 7px 0;
108+
border: 1px black solid;
96109
}
97110

98111
.title {
99112
margin: 10px;
100-
font-size: 35px;
113+
font-size: 28px;
101114
color: #206d31;
102115
font-weight: bolder;
103-
width: 100%;
116+
width: max-content;
104117
text-align: center;
105118
}
119+
.best {
120+
padding-top: 4px;
121+
font-size: 16px;
122+
text-align: end;
123+
justify-content: right;
124+
}
106125

107-
.flex {
126+
.flex-column,
127+
.flex-row {
108128
display: flex;
109-
justify-content: center;
110129
align-items: center;
130+
}
131+
132+
.flex-column {
133+
justify-content: center;
111134
flex-direction: column;
112135
}
136+
137+
.flex-row {
138+
flex-direction: row;
139+
}
140+
141+
.m-4 {
142+
margin: 0 16px;
143+
}
113144
</style>
114145

115146
</head>
116147

117148
<body>
118-
<div class="flex">
119-
<div class="input-area">
149+
<div class="flex-column">
150+
<div class="title">
120151
Let's test your typing speed.
152+
<div id="best" class="best p-4 border-top"></div>
121153
</div>
122154
<em>Press enter to reset at any point of time.</em>
123155
<div id="letter" class="letter"></div>
124156
<div class="input-area">
125-
<input class="input-field" placeholder="Follow the alphabets shown above" type="text" id="typeHere"><input
126-
type="reset" id="reset" value="reset" class="reset">
157+
<input class="input-field" placeholder="Follow the alphabets shown above" type="text" id="typeHere">
158+
<input type="reset" id="reset" value="Reset" class="reset">
127159
</div>
128-
<div>
160+
<div class="flex-row">
129161
<input type="checkbox" name="Shuffle" id="shuffle" />
130162
<label for="Shuffle"> Shuffle & Reset </label>
131163
</div>
132164
</div>
133165
<div id="results" class="results"></div>
166+
<div class="warning">
167+
</div>
134168
<div id="info"></div>
135169

136170
</body>

0 commit comments

Comments
 (0)