-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
56 lines (54 loc) · 1.9 KB
/
Copy pathscript.js
File metadata and controls
56 lines (54 loc) · 1.9 KB
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
const quotes=[
"Practice makes perfect in typing.",
"The quick brown fox jumps over the lazy dog.",
"Typing speed grows with consistency.",
"Focus on accuracy before speed."
];
const quote=document.getElementById('quote');
const input=document.getElementById('input');
const timeEl=document.getElementById('time');
const wpmEl=document.getElementById('wpm');
const cpmEl=document.getElementById('cpm');
const accEl=document.getElementById('accuracy');
let timer,remaining,startTime;
function newQuote(){quote.textContent=quotes[Math.floor(Math.random()*quotes.length)]}
function restart(){
newQuote();
input.value='';
remaining=parseInt(document.getElementById('timeSelect').value);
timeEl.textContent=remaining;
clearInterval(timer);
timer=setInterval(()=>{
remaining--;
timeEl.textContent=remaining;
if(remaining<=0){clearInterval(timer);saveScore();input.disabled=true;}
},1000);
input.disabled=false;
}
function saveScore(){
let best=JSON.parse(localStorage.getItem('typinglo_scores')||'[]');
best.push(parseInt(wpmEl.textContent));
best=best.sort((a,b)=>b-a).slice(0,5);
localStorage.setItem('typinglo_scores',JSON.stringify(best));
renderScores();
}
function renderScores(){
let best=JSON.parse(localStorage.getItem('typinglo_scores')||'[]');
document.getElementById('scores').innerHTML=best.map(x=>'<li>'+x+' WPM</li>').join('');
}
input.addEventListener('input',()=>{
let typed=input.value;
let original=quote.textContent;
let correct=0;
for(let i=0;i<typed.length;i++){if(typed[i]===original[i]) correct++;}
let acc=typed.length?Math.round(correct/typed.length*100):100;
accEl.textContent=acc;
let elapsed=(parseInt(document.getElementById('timeSelect').value)-remaining)/60;
if(elapsed>0){
wpmEl.textContent=Math.round(typed.trim().split(/\s+/).length/elapsed);
cpmEl.textContent=Math.round(typed.length/elapsed);
}
});
document.getElementById('themeBtn').onclick=()=>document.body.classList.toggle('dark');
renderScores();
restart();