03-01 Let's Practice Vocab
TOPIC: selectAnswer()
関数の定義
CHECK
- クリックした Element の最初の Child を取得。つまり
<div class="answer-container">
の中にある最初の Child の<div class="answer-box">
を取得する
- correct 変数に
<div class="answer-box">
に設定した"data-correct=true"があるかをさがす。もしあれば"data-correct=true"の"true"を correct 変数に格納する
- answerEl にある 4 択選択肢の Element をループする
answerEl.children[0]
で最初の Child エレメントを取得。つまり<div class="answer-box"></div>
- setStatusClass()を呼び出し、
<div class="answer-box">
にsetStatusClass
を使って<div class="answer-box" class="correct">
もしくは<div class="answer-box" class="wrong">
のように class を追加する。これによって、あとで CSS で色を変更することができる
setStatusClass()
関数を定義する(class を追加したい Element と正解かを判断する correct 変数を渡す)
clearStatusClass()
を呼び出し、すでについている class を消す(clearStatus()
関数はあとで定義する)
- もし、correct が true なら、その Element に class="correct"を追加し、そうでなければ Element に class="wrong"を追加する
clearStatusClass()
関数を定義する
- その element についている"correct", "wrong" class を消す Function
console.log
でanswerBoxEl.dataset.correct
を表示し、確認しよう
const startBtnEl = document.getElementById("start-btn")
const questionAnswersContainerEl = document.getElementById(
"question-answers-container"
)
const headerContainerEl = document.getElementById("header-container")
const headerSmallEl = document.getElementById("header-small")
const nextBtnEl = document.getElementById("next-btn")
const answersEl = document.getElementById("answers")
const jpTextEl = document.getElementById("jp-text")
const enTextEl = document.getElementById("en-text")
let shuffledQuestions
let currentQuestionIndex
startBtnEl.addEventListener("click", startGame)
function startGame() {
startBtnEl.classList.add("hide")
shuffledQuestions = questions.sort(() => Math.random() - 0.5)
currentQuestionIndex = 0
questionAnswersContainerEl.classList.remove("hide")
headerContainerEl.style["display"] = "none"
headerSmallEl.classList.remove("hide")
setNextQuestion()
}
function setNextQuestion() {
resetState()
showQuestion(shuffledQuestions[currentQuestionIndex])
}
function resetState() {
nextBtnEl.classList.add("hide")
while (answersEl.children[0]) {
answersEl.removeChild(answersEl.children[0])
}
}
function showQuestion(question) {
jpTextEl.innerText = question.jp
enTextEl.innerText = question.en
question.answers.forEach((answer, idx) => {
const el = `
<div class="answer-box">
<div class="answer-number">
<span>${idx + 1}</span>
</div>
<h3 id="answer1" class="answer" data-number="1">${answer.text}</h3>
</div>
`
const div = document.createElement("div")
div.classList.add("answer-container")
div.innerHTML = el
if (answer.correct) {
div.children[0].dataset.correct = answer.correct
}
div.addEventListener("click", selectAnswer)
answersEl.appendChild(div)
})
}
function selectAnswer(e) {
const selectedAnswer = e.currentTarget.children[0]
const correct = selectedAnswer.dataset.correct
Array.from(answersEl.children).forEach((answerEl) => {
const answerBoxEl = answerEl.children[0]
setStatusClass(answerBoxEl, answerBoxEl.dataset.correct)
console.log("answerBoxEl.dataset.correct", answerBoxEl.dataset.correct)
})
}
function setStatusClass(el, correct) {
clearStatusClass(el)
if (correct) {
el.classList.add("correct")
} else {
el.classList.add("wrong")
}
}
function clearStatusClass(el) {
el.classList.remove("correct")
el.classList.remove("wrong")
}
const questions = [
{
target_num: 1,
jp: "現代の科学技術は私たちの生活を大いに[向上させた]。",
en: "Modern technology has greatly ( ) our lives.",
answers: [
{
text: "considered",
correct: false,
},
{
text: "improved",
correct: true,
},
{
text: "concerned",
correct: false,
},
{
text: "included",
correct: false,
},
],
},
{
target_num: 2,
jp: "生活の質はエネルギー使用に直接[関連し]てはいない。",
en: "The quality of life is not directly ( ) to energy use.",
answers: [
{
text: "produced",
correct: false,
},
{
text: "related",
correct: true,
},
{
text: "provided",
correct: false,
},
{
text: "improved",
correct: false,
},
],
},
{
target_num: 3,
jp: "言語はすべて私たちに社会に関する貴重な情報を[与える]ことができる。",
en: "All languages can ( ) us with valuable information about society.",
answers: [
{
text: "provide",
correct: true,
},
{
text: "include",
correct: false,
},
{
text: "relate",
correct: false,
},
{
text: "concern",
correct: false,
},
],
},
]