[miniProjects] 46_Quiz App

๋ณด๋ฆฌยท2023๋…„ 8์›” 10์ผ
0

miniProjects

๋ชฉ๋ก ๋ณด๊ธฐ
46/47

46_Quiz App

๐Ÿ’ป ์ฃผ์ œ : ํ€ด์ฆˆ๋ฅผ ํ‘ผ ํ›„ ๋ช‡ ๊ฐœ๋ฅผ ๋งž์ท„๋Š”์ง€ ๋ณด์—ฌ์คŒ.

โœจJS

๐Ÿ˜ถ ํ€ด์ฆˆ ๋ฐ์ดํ„ฐ

const quizData = [
  {
      question: "์›น ๋ธŒ๋ผ์šฐ์ €์—์„œ ์‹คํ–‰๋˜๋Š” ์–ธ์–ด๋Š”?",
      a: "Java",
      b: "C",
      c: "Python",
      d: "JavaScript",
      correct: "d",
  },
  {
      question: "CSS๋Š” ๋ฌด์—‡์„ ์˜๋ฏธํ•˜๋‚˜?",
      a: "Central Style Sheets",
      b: "Cascading Style Sheets",
      c: "Cascading Simple Sheets",
      d: "Cars SUVs Sailboats",
      correct: "b",
  },
  {
      question: "HTML๋Š” ๋ฌด์—‡์„ ์˜๋ฏธํ•˜๋‚˜",
      a: "Hypertext Markup Language",
      b: "Hypertext Markdown Language",
      c: "Hyperloop Machine Language",
      d: "Helicopters Terminals Motorboats Lamborginis",
      correct: "a",
  },
  {
      question: "JavaScript๊ฐ€ ์ถœ์‹œ๋œ ์—ฐ๋„๋Š”?",
      a: "1996",
      b: "1995",
      c: "1994",
      d: "none of the above",
      correct: "b",
  },
];

๐Ÿ˜ถ ํ€ด์ฆˆ ํ™”๋ฉด๊ณผ ๋‹ต์„ ์„ ํƒํ–ˆ์„ ๋•Œ

// ํ˜„์žฌ ํ€ด์ฆˆ์˜ ์ธ๋ฑ์Šค๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๋ณ€์ˆ˜
let currentQuiz = 0
// ์‚ฌ์šฉ์ž์˜ ์ ์ˆ˜
let score = 0

loadQuiz()

function loadQuiz() {
  deselectAnswers()

  const currentQuizData = quizData[currentQuiz]

  questionEl.innerText = currentQuizData.question
  a_text.innerText = currentQuizData.a
  b_text.innerText = currentQuizData.b
  c_text.innerText = currentQuizData.c
  d_text.innerText = currentQuizData.d
}

function deselectAnswers() {
  answerEls.forEach(answerEl => answerEl.checked = false)
}

function getSelected() {
  let answer

  answerEls.forEach(answerEl => {
      if(answerEl.checked) {
          answer = answerEl.id
      }
  })

  return answer
}
  • deselectAnswers(): ๋‹ต์•ˆ ์„ ํƒ ์ƒํƒœ๋ฅผ ์ดˆ๊ธฐํ™”

  • const currentQuizData = quizData[currentQuiz]
    currentQuiz์— ํ•ด๋‹นํ•˜๋Š” ํ€ด์ฆˆ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜จ๋‹ค.

  • innerText
    ๊ฐ questionEl๊ณผ a_text, b_text, c_text, d_text ์š”์†Œ์— ํ€ด์ฆˆ ๋ฐ์ดํ„ฐ์˜ ์งˆ๋ฌธ๊ณผ ๊ฐ ์„ ํƒ์ง€๋ฅผ ํ• ๋‹นํ•œ๋‹ค.

  • answerEls.forEach(answerEl => answerEl.checked = false)
    answerEls ๋ฐฐ์—ด์˜ ๋ชจ๋“  ์š”์†Œ์˜ ์„ ํƒ ์ƒํƒœ๋ฅผ ํ•ด์ œํ•œ๋‹ค.
    ์‚ฌ์šฉ์ž๊ฐ€ ์ƒˆ ํ€ด์ฆˆ๋กœ ๋„˜์–ด๊ฐˆ ๋•Œ ์ด์ „ ํ€ด์ฆˆ์˜ ์„ ํƒ์„ ์ดˆ๊ธฐํ™”ํ•œ๋‹ค.

  • function getSelected() { ... }:
    ์„ ํƒ๋œ ๋‹ต์„ ๊ฐ€์ ธ์˜จ๋‹ค. ์„ ํƒ๋œ ๋‹ต์•ˆ์˜ id๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

  • answerEls.forEach(answerEl => { ... })
    answerEls ๋ฐฐ์—ด์˜ ๊ฐ ์š”์†Œ์— ๋Œ€ํ•ด ๋ฐ˜๋ณต๋ฌธ์„ ์‹คํ–‰ํ•œ๋‹ค.

  • if(answerEl.checked) { ... }
    ํ˜„์žฌ ์š”์†Œ๊ฐ€ ์„ ํƒ๋˜์–ด ์žˆ๋Š”์ง€๋ฅผ ํ™•์ธํ•˜๊ณ , ์„ ํƒ๋˜์–ด ์žˆ๋‹ค๋ฉด ํ•ด๋‹น ์š”์†Œ์˜ id๋ฅผ answer ๋ณ€์ˆ˜์— ํ• ๋‹นํ•œ๋‹ค.

๐Ÿ˜ถ ์ œ์ถœํ•˜๊ธฐ ๋ฒ„ํŠผ

submitBtn.addEventListener('click', () => {
  const answer = getSelected()
  
  if(answer) {
      if(answer === quizData[currentQuiz].correct) {
          score++
      }

      currentQuiz++

      if(currentQuiz < quizData.length) {
          loadQuiz()
      } else {
          quiz.innerHTML = `
              <h2>๋‹น์‹ ์€ ${score}/${quizData.length} ๋ฌธ์ œ๋ฅผ ๋งžํ˜”์Šต๋‹ˆ๋‹ค!</h2>

              <button>๋‹ค์‹œํ•˜๊ธฐ</button>
          `
      }
  }
})
  • const answer = getSelected()
    getSelected() ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ์‚ฌ์šฉ์ž๊ฐ€ ์„ ํƒํ•œ ๋‹ต์„ ๊ฐ€์ ธ์˜จ๋‹ค.

  • if(answer === quizData[currentQuiz].correct) { ... }
    ์‚ฌ์šฉ์ž๊ฐ€ ์„ ํƒํ•œ ๋‹ต answer์™€ ํ˜„์žฌ ํ€ด์ฆˆ์˜ ์˜ฌ๋ฐ”๋ฅธ ๋‹ต quizData[currentQuiz].correct๋ฅผ ๋น„๊ตํ•œ๋‹ค.
    ๋งŒ์•ฝ ๋งž์•˜๋‹ค๋ฉด score๊ฐ€ ์ฆ๊ฐ€๋œ๋‹ค.

  • currentQuiz++: ๋‹ค์Œ ํ€ด์ฆˆ๋กœ ๋„˜์–ด๊ฐˆ ์ˆ˜ ์žˆ๋„๋ก currentQuiz๋ฅผ ์ฆ๊ฐ€์‹œํ‚จ๋‹ค.

  • if(currentQuiz < quizData.length) { ... }
    ๋งŒ์•ฝ ์•„์ง ๋ชจ๋“  ํ€ด์ฆˆ๋ฅผ ๋‹ค ํ’€์ง€ ์•Š์•˜๋‹ค๋ฉด, ๋‹ค์Œ ํ€ด์ฆˆ๋ฅผ ๋กœ๋“œํ•˜๊ธฐ ์œ„ํ•ด loadQuiz() ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค.

  • quiz.innerHTML = ...
    quiz ์š”์†Œ์˜ ๋‚ด์šฉ์„ ๋ณ€๊ฒฝํ•˜์—ฌ ์ตœ์ข… ์ ์ˆ˜์™€ "๋‹ค์‹œํ•˜๊ธฐ" ๋ฒ„ํŠผ์„ ์ถœ๋ ฅํ•œ๋‹ค.

  • location.reload()
    "๋‹ค์‹œํ•˜๊ธฐ" ๋ฒ„ํŠผ์„ ํด๋ฆญํ•˜๋ฉด ํ˜„์žฌ ํŽ˜์ด์ง€๋ฅผ ์ƒˆ๋กœ๊ณ ์นจํ•˜์—ฌ ํ€ด์ฆˆ๋ฅผ ๋‹ค์‹œ ์‹œ์ž‘ํ•  ์ˆ˜ ์žˆ๋‹ค.

profile
์ •์‹ ์ฐจ๋ ค ์ด ๊ฐ๋ฐ•ํ•œ ์„ธ์ƒ์†์—์„œ

0๊ฐœ์˜ ๋Œ“๊ธ€