-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.js
95 lines (75 loc) · 2.48 KB
/
script.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
const ORGINTEXT = document.querySelector("#origin-text");
const SMALLTEXT = document.querySelector("#small-text");
const ORGINTEXTLABEL = document.querySelector("#origin-text-label");
const INPUT_ENTERED = document.querySelector("#entered-text");
const CLOCK = document.querySelector("#clock");
const BTN_RESET = document.querySelector("#reset-btn");
let x =[0,0,0,0];
let started = false;
let interval =0;
function runTimer(){
CLOCK.innerHTML = addZeroToFirst(x[0])+":"+addZeroToFirst(x[1])+":"+addZeroToFirst(x[2]);
x[0]=Math.floor((x[3]/100)/60);
x[1]=Math.floor((x[3]/100) - (x[0]*60));
x[2]=Math.floor((x[3]) -(x[1]*100) -(x[0]*6000))
x[3]++;
}
function addZeroToFirst(value){
return value<=9? "0"+value : value;
}
function startTest(e){
let enteredTextLenght = INPUT_ENTERED.value.length;
if(enteredTextLenght === 0 && !started){
interval = setInterval(runTimer,10);
started = true;
}
}
function checkSpell(){
if(!started)
startTest();
let enteredText = INPUT_ENTERED.value;
let originText = ORGINTEXTLABEL.innerHTML;
let currentMatch =originText.substring(0,enteredText.length);
if(enteredText === originText) {
INPUT_ENTERED.style.borderColor ="rgb(49, 182, 49)";
clearInterval(interval);
}
else
{
if(currentMatch === enteredText){
INPUT_ENTERED.style.borderColor ="blue";
}
else{
INPUT_ENTERED.style.borderColor ="orange";
}
}
}
function restTest(){
x =[0,0,0,0];
started = false;
clearInterval(interval);
interval =0;
CLOCK.innerHTML = "00:00:00";
INPUT_ENTERED.value = "";
INPUT_ENTERED.style.borderColor ="gray";
}
function saveOriginText(e){
if(e.keyCode=== 13){
ORGINTEXTLABEL.innerHTML=ORGINTEXT.value;
ORGINTEXT.style.display="none";
SMALLTEXT.style.display="none";
ORGINTEXTLABEL.style.display="block";
e.preventDefault();
}
}
function modifyOrginalText(){
ORGINTEXT.style.display="block";
SMALLTEXT.style.display="block";
ORGINTEXT.value=ORGINTEXTLABEL.innerHTML;
ORGINTEXTLABEL.style.display="none";
}
INPUT_ENTERED.addEventListener("keypress",startTest,false);
INPUT_ENTERED.addEventListener("keyup",checkSpell,false);
BTN_RESET.addEventListener("click",restTest,false);
ORGINTEXTLABEL.addEventListener("click",modifyOrginalText,false);
ORGINTEXT.addEventListener("keypress",saveOriginText,false)