All checks were successful
Deploy Online Pages / Pages-Deployment (push) Successful in 26s
282 lines
10 KiB
JavaScript
282 lines
10 KiB
JavaScript
let wordClass = "noun";
|
||
let target = words();
|
||
let targetLen = target.length;
|
||
//単語リストの長さ
|
||
console.log(targetLen);
|
||
|
||
let body = document.getElementsByTagName("body")[0];
|
||
let clsChg = document.getElementById("clsChg");
|
||
let display = document.getElementById("display");
|
||
let word = document.getElementById("word");
|
||
let pronunc = document.getElementById("pronunc");
|
||
let ans = [document.getElementById("ans_0"), document.getElementById("ans_1"), document.getElementById("ans_2"), document.getElementById("ans_3")]
|
||
let result = document.getElementById("result");
|
||
let scoreElemA = document.getElementById("scoreA");
|
||
let scoreElemB = document.getElementById("scoreB");
|
||
let corrCntElemA = document.getElementById("corrCntA");
|
||
let wrongCntElemA = document.getElementById("wrongCntA");
|
||
let corrCntElemB = document.getElementById("corrCntB");
|
||
let wrongCntElemB = document.getElementById("wrongCntB");
|
||
let perfect = document.getElementById("perfect");
|
||
let imperfect = document.getElementById("imperfect");
|
||
let replay = document.getElementById("replay");
|
||
let startSound = document.getElementById("startSound");
|
||
let corrSound = document.getElementById("corrSound");
|
||
let corrSound2 = document.getElementById("corrSound2");
|
||
let wrongSound = document.getElementById("wrongSound");
|
||
corrSound.volume = 0.5;
|
||
wrongSound.volume = 0.5;
|
||
|
||
let curWordIdx;
|
||
let curCorrIdx;
|
||
//quiz()の返り値で指定される問題語番号と四択正答番号
|
||
let isClicked = [false, false, false, false];
|
||
let wordIdx;
|
||
let tmpWordIdx;
|
||
//関数内の問題語番号の運用
|
||
let wordTxt;
|
||
//問題語のテキスト
|
||
let corrDict;
|
||
//問題語の個人情報リスト
|
||
let corrIdx;
|
||
let wrongIdx = [];
|
||
let tmpWrongIdx;
|
||
let cnt = 0;
|
||
let corrCnt = 0;
|
||
let wrongCnt = 0;
|
||
//正答数・誤答数の計上(のちElemへ)
|
||
let wrongs = [];
|
||
let wrongsLen;
|
||
let mistakenWordIdx;
|
||
let mistakenWordPronunc;
|
||
let mistakenWordPronuncLen;
|
||
let mistakenWordPronuncTxt;
|
||
let wrongsTxt;
|
||
|
||
function randElem(arr) {
|
||
//与配列からランダムに要素を一つ取り出して返す関数
|
||
return arr[Math.floor(Math.random() * arr.length)];
|
||
}
|
||
function setQuiz() {
|
||
scoreElemA.style.display = "block";
|
||
display.style.display = "table";
|
||
result.style.display = "none";
|
||
replay.style.display = "none";
|
||
}
|
||
function removeQuiz() {
|
||
display.style.display = "none";
|
||
result.style.display = "block";
|
||
replay.style.display = "block";
|
||
}
|
||
|
||
function quiz() {
|
||
if (cnt == 20) {
|
||
//終了処理
|
||
removeQuiz();
|
||
cnt = 0;
|
||
wrongsLen = wrongs.length;
|
||
if (wrongsLen == 0) {
|
||
//perfect画面
|
||
perfect.style.display = "block";
|
||
imperfect.style.display = "none";
|
||
scoreElemA.style.display = "none";
|
||
scoreElemB.style.display = "none";
|
||
body.style.animation = "gaming 2s linear infinite";
|
||
result.style.animation = "gaming 2s linear infinite";
|
||
perfect.style.animation = "gaming 2s linear infinite";
|
||
return [null, null];
|
||
} else {
|
||
//imperfect画面
|
||
perfect.style.display = "none";
|
||
imperfect.style.display = "block";
|
||
scoreElemA.style.display = "none";
|
||
scoreElemB.style.display = "block";
|
||
corrCntElemB.innerText = corrCnt;
|
||
wrongCntElemB.innerText = wrongCnt;
|
||
wrongsTxt = "";
|
||
for (let i = 0; i < wrongsLen; ++i) {
|
||
//誤答の一覧の表示
|
||
mistakenWordIdx = wrongs[i][0];
|
||
//間違い方リストから正答番号を取り出した
|
||
mistakenWordPronunc = target[mistakenWordIdx]["pronunc"];
|
||
mistakenWordPronuncLen = mistakenWordPronunc.length;
|
||
console.log(mistakenWordPronunc);
|
||
mistakenWordPronuncTxt = "‘" + mistakenWordPronunc[0] + "’";
|
||
if (mistakenWordPronuncLen != 0) {
|
||
for (let i = 1; i < mistakenWordPronuncLen; ++i) {
|
||
//多義語の処理らしい
|
||
if (i == mistakenWordPronuncLen - 1) {
|
||
mistakenWordPronuncTxt += "</span> or <span style=\"color: red;\">'" + mistakenWordPronunc[i] + "'";
|
||
} else {
|
||
mistakenWordPronuncTxt += "</span>, <span style=\"color: red;\">'" + mistakenWordPronunc[i] + "'";
|
||
}
|
||
}
|
||
}
|
||
wrongsTxt += "<span style=\"color: red;\">‘" + target[mistakenWordIdx]["word"] + "’</span>"
|
||
+ " 是 <span style=\"color: red;\">"
|
||
+ mistakenWordPronuncTxt + "</span>,不是 ‘" + wrongs[i][1] + "’。<br>"
|
||
}
|
||
imperfect.innerHTML = wrongsTxt.slice(0, -4);
|
||
return [null, null];
|
||
}
|
||
} else {
|
||
//継続処理
|
||
setQuiz();
|
||
tmpWordIdx = Math.floor(Math.random() * targetLen);
|
||
while (tmpWordIdx == wordIdx) {
|
||
//連続同一問題を排除する
|
||
tmpWordIdx = Math.floor(Math.random() * targetLen);
|
||
}
|
||
wordIdx = tmpWordIdx;
|
||
corrIdx = Math.floor(Math.random() * 4);
|
||
console.log(corrIdx);
|
||
//問題語と正答番号をリフレッシュした
|
||
for (let i = 0; i < 4; ++i) {
|
||
//誤答選択肢を作る
|
||
if (i == corrIdx) continue;
|
||
tmpWrongIdx = Math.floor(Math.random() * targetLen);
|
||
while (wrongIdx.includes(tmpWrongIdx) || tmpWrongIdx == wordIdx) {
|
||
//以前の誤答集に含まれる、または正答と同一であるような誤答を省く
|
||
tmpWrongIdx = Math.floor(Math.random() * targetLen);
|
||
}
|
||
wrongIdx.push(tmpWrongIdx);
|
||
//誤答集に今回使った誤答を追加した
|
||
ans[i].innerText = randElem(target[tmpWrongIdx]["pronunc"]);
|
||
//ランダム列、ピンイン行の要素を誤答ゾーンに代入した
|
||
}
|
||
wrongIdx = [];
|
||
//誤答集がリセットされた
|
||
corrDict = target[wordIdx]
|
||
wordTxt = corrDict["word"];
|
||
//実装されなかったらしいジェンダーの表示機能
|
||
if (corrDict["gender_is_distinctive"]) wordTxt += " (" + corrDict["gender"] + ")";
|
||
word.innerText = wordTxt;
|
||
pronunc.innerText = "" + corrDict["meaning"] + "";
|
||
ans[corrIdx].innerText = randElem(corrDict["pronunc"]);
|
||
//多義語ならば意味の一つだけを表示する仕様だったらしい
|
||
cnt++;
|
||
return [wordIdx, corrIdx]
|
||
}
|
||
}
|
||
function start() {
|
||
//初期値設定
|
||
[curWordIdx, curCorrIdx] = quiz();
|
||
startSound.currentTime = 0;
|
||
startSound.play();
|
||
corrCnt = 0;
|
||
wrongCnt = 0;
|
||
wrongs = [];
|
||
isClicked = [false, false, false, false];
|
||
corrCntElemA.innerText = 0;
|
||
wrongCntElemA.innerText = 0;
|
||
body.style.animation = "";
|
||
result.style.animation = "";
|
||
}
|
||
start();
|
||
ans[0].addEventListener("click", () => {
|
||
if (curCorrIdx == 0) {
|
||
//正解の場合
|
||
if (cnt == 20) {
|
||
corrSound2.play();
|
||
} else {
|
||
corrSound.currentTime = 0;
|
||
corrSound.play();
|
||
}
|
||
corrCnt++;
|
||
corrCntElemA.innerText = corrCnt;
|
||
[curWordIdx, curCorrIdx] = quiz();
|
||
isClicked = [false, false, false, false];
|
||
} else {
|
||
//間違いの場合
|
||
wrongSound.currentTime = 0;
|
||
wrongSound.play();
|
||
if (isClicked[0]) return;
|
||
//同じ誤答の間違いをカウントしないようにした
|
||
wrongCnt++;
|
||
wrongCntElemA.innerText = wrongCnt;
|
||
isClicked[0] = true;
|
||
wrongs.push([curWordIdx, ans[0].textContent]);
|
||
//間違い方リストに正答番号と選んだ誤答が追加された
|
||
}
|
||
})
|
||
ans[1].addEventListener("click", () => {
|
||
if (curCorrIdx == 1) {
|
||
if (cnt == 20) {
|
||
corrSound2.play();
|
||
} else {
|
||
corrSound.currentTime = 0;
|
||
corrSound.play();
|
||
}
|
||
corrCnt++;
|
||
corrCntElemA.innerText = corrCnt;
|
||
[curWordIdx, curCorrIdx] = quiz();
|
||
isClicked = [false, false, false, false];
|
||
} else {
|
||
wrongSound.currentTime = 0;
|
||
wrongSound.play();
|
||
if (isClicked[1]) return;
|
||
wrongCnt++;
|
||
wrongCntElemA.innerText = wrongCnt;
|
||
isClicked[1] = true;
|
||
wrongs.push([curWordIdx, ans[1].textContent]);
|
||
}
|
||
})
|
||
ans[2].addEventListener("click", () => {
|
||
if (curCorrIdx == 2) {
|
||
if (cnt == 20) {
|
||
corrSound2.play();
|
||
} else {
|
||
corrSound.currentTime = 0;
|
||
corrSound.play();
|
||
}
|
||
corrCnt++;
|
||
corrCntElemA.innerText = corrCnt;
|
||
[curWordIdx, curCorrIdx] = quiz();
|
||
isClicked = [false, false, false, false];
|
||
} else {
|
||
wrongSound.currentTime = 0;
|
||
wrongSound.play();
|
||
if (isClicked[2]) return;
|
||
wrongCnt++;
|
||
wrongCntElemA.innerText = wrongCnt;
|
||
isClicked[2] = true;
|
||
wrongs.push([curWordIdx, ans[2].textContent]);
|
||
}
|
||
})
|
||
ans[3].addEventListener("click", () => {
|
||
if (curCorrIdx == 3) {
|
||
if (cnt == 20) {
|
||
corrSound2.play();
|
||
} else {
|
||
corrSound.currentTime = 0;
|
||
corrSound.play();
|
||
}
|
||
corrCnt++;
|
||
corrCntElemA.innerText = corrCnt;
|
||
[curWordIdx, curCorrIdx] = quiz();
|
||
isClicked = [false, false, false, false];
|
||
} else {
|
||
wrongSound.currentTime = 0;
|
||
wrongSound.play();
|
||
if (isClicked[3]) return;
|
||
wrongCnt++;
|
||
wrongCntElemA.innerText = wrongCnt;
|
||
isClicked[3] = true;
|
||
wrongs.push([curWordIdx, ans[3].textContent]);
|
||
}
|
||
})
|
||
replay.addEventListener("click", start)
|
||
clsChg.addEventListener("click", () => {
|
||
if (wordClass == "noun") {
|
||
wordClass = "verb";
|
||
clsChg.innerText = "V";
|
||
} else {
|
||
wordClass = "noun";
|
||
clsChg.innerText = "N";
|
||
}
|
||
target = words(wordClass);
|
||
targetLen = target.length;
|
||
console.log(targetLen);
|
||
cnt = 0;
|
||
start();
|
||
}) |