-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
85 lines (72 loc) · 2.23 KB
/
main.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
const wordgame = {
question: "りんごの英単語は?",
correct: "apple",
evaluation: function (answer) {
return wordgame.correct == answer;
}
};
const questionElement = document.getElementById("question");
questionElement.innerText = wordgame.question;
let inputKeys = "";
function showExeCount(event) {
let inputKey;
if (event.key) {
inputKey = event.key;
} else {
inputKey = event.target.innerText;
}
const resultEelement = document.getElementById("result");
resultEelement.className = "result-hidden";
//何も要素がないれば処理しない
if (!inputKey) {
return;
}
// enter, delete 以外の2文字以上の入力は使わない
if (inputKey != "Enter" && inputKey != "Delete" && inputKey.length >= 2) {
return;
}
// 数字も扱わない
if (!Number.isNaN(Number(inputKey))) {
return;
}
if (inputKey == "Enter") {
// 正誤判定
const resultEelement = document.getElementById("result");
resultEelement.className = "result";
if (wordgame.evaluation(inputKeys)) {
resultEelement.innerText = "正解";
} else {
resultEelement.innerText = "不正解";
}
return;
}
if (inputKey == "Delete") {
let deleteLast = inputKeys.substring(0, inputKeys.length - 1);
inputKeys = deleteLast;
} else {
inputKeys += inputKey;
}
const timeElement = document.getElementById("keyboardinput");
timeElement.innerText = inputKeys;
}
document.onkeydown = showExeCount;
function showAlphabet() {
const virtualKyboard = document.getElementById("virtualkeyboard");
const aCharCode = 'a'.charCodeAt(0);
for (let i = aCharCode; i < aCharCode + 26; i++) {
const letter = String.fromCharCode(i);
const childButton = document.createElement("button");
childButton.innerText = letter;
childButton.onclick = showExeCount;
virtualKyboard.appendChild(childButton);
}
const enterButton = document.createElement("button");
enterButton.innerText = "Enter";
enterButton.onclick = showExeCount;
virtualKyboard.appendChild(enterButton);
const deleteButton = document.createElement("button");
deleteButton.innerText = "Delete";
deleteButton.onclick = showExeCount;
virtualKyboard.appendChild(deleteButton);
}
showAlphabet();