-
Notifications
You must be signed in to change notification settings - Fork 10
/
keyExercise.js
141 lines (111 loc) · 3.1 KB
/
keyExercise.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
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
function changeClass(node, command) {
var operator = command.charAt(0),
newClass = command.slice(1),
oldClass = (node.className || '').split(' ');
if (operator == '+') {
oldClass.push(newClass);
node.className = oldClass.join(' ');
}
else if (operator == '-') {
do oldClass.splice( oldClass.indexOf(newClass), 1 );
while (oldClass.indexOf(newClass) != -1)
node.className = oldClass.join(' ');
}
else node.className = command;
}
var keyboard = (function (){
var keyboard = document.getElementById('keyboardMap');
var key = {};
var mapper = { ' ': ' ' };
for (var i=0; i<3; i++) {
var row = keyboard.children[i].children;
for (var j=0, l=row.length; j<l; j++) {
var alphabet = row[j].title;
key[alphabet] = row[j];
mapper[alphabet] = row[j].textContent;
}
}
function press(keyName) {
setTimeout(
function(){ changeClass(key[keyName], "-press"); },
150
);
changeClass(key[keyName], "+press");
}
function hint(keyName) {
key.hint && changeClass(key[key.hint], "-hint");
changeClass(key[keyName], "+hint");
key.hint = keyName;
}
return {
'press': press,
'hint': hint,
'mapper': mapper,
'key': key
};
})();
var questCheck = (function() {
var characterTable = document.getElementById("character");
var characterArray = characterTable.textContent.split('\n');
var questBar = document.getElementById('questAlphabet').children,
nowCharacter;
function compare(string) {
for (var i=0, l=nowCharacter.length; i<l; i++) {
if (string.charAt(i) !== nowCharacter.charAt(i)) break;
}
return i;
}
function indicate(index, wrong) {
var l = nowCharacter.length;
for (var i=0; i<index; i++) {
changeClass(questBar[i+1], "right");
}
var hintCharIndex = i;
for (; i<wrong; i++) changeClass(questBar[i+1], "wrong");
changeClass(questBar[++i], "cursor");
for (; i<l; i++) changeClass(questBar[i+1], "");
return hintCharIndex;
}
function setNewCharacter(characterString) {
nowCharacter = characterString.slice(1);
questBar[0].textContent = characterString.charAt(0);
for (var i=1, l=questBar.length; i<l; i++) {
questBar[i].textContent = keyboard.mapper[
characterString.charAt(i) || ' '
];
changeClass(questBar[i], "");
}
}
setNewCharacter( characterArray[
Math.floor( Math.random() * (characterArray.length-2) ) + 1
]);
function check(string) {
var index = compare(string);
if (index >= nowCharacter.length){
setNewCharacter( characterArray[Math.floor(
Math.random() * (characterArray.length-2)
) + 1]);
index = -1;
string = '';
}
keyboard.hint( nowCharacter.charAt(
indicate(index, string.length)
));
return index == -1;
}
return check;
})();
document.getElementById('inputBar').oninput = function(){
var string = this.value;
if (/[^a-y]/.test(string)) {
this.value = '';
string = '';
}
string && keyboard.press(string.slice(-1));
if (questCheck(string)) this.value = '';
};
document.getElementById('fontPx').children[1].onchange = function(){
document.body.style.fontSize = this.value + 'px';
};
document.getElementById('inputBar').focus();
document.getElementById('inputBar').select();