02-03 Let's Practice Vocab
TOPIC: setNextQuestion()
関数の定義
CHECK
showQuestion()
- Question オブジェクトの answers プロパティ(array)にある一つ一つの anwer をループし、1 個の Answer HTML を文字で作成し、ところどころ Question オブジェクトのデータを挿入する
console.log
でできて文字列を確認
- 新しい div Element を作成する
- その div に CSS のためにその Element に class を追加する
- その Element の HTML を先程作った HTML 文字列を挿入する
- 4 択の新しくできた Answer Element が表示されるかを確認する
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) {
console.log("question", 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>
`
console.log(el)
const div = document.createElement("div")
div.classList.add("answer-container")
div.innerHTML = el
answersEl.appendChild(div)
})
}
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,
},
],
},
]