Projects

Simple Quiz App

2021·1 min read

JavaScript
HTML
CSS
Link (opens in a new tab)GitHub (opens in a new tab)

A multiple-choice quiz that runs entirely from one array. Answer, submit, move to the next question, and get a score at the end.

A multiple-choice quiz that runs entirely from one array. Answer, submit, move to the next question, and get a score at the end.

Live demo →

Stack#

Plain HTML, CSS and JavaScript. No framework, no router, no build step, no dependencies. Deployed to GitHub Pages.

Questions are data, not markup#

Every question is an object with the text, four options and the key of the correct answer:

const quizData = [
  {
    question: 'Which language runs in a web browser?',
    a: 'Java',
    b: 'C',
    c: 'Python',
    d: 'JavaScript',
    correct: 'd',
  },
  // ...
]

The markup is written once: four radio inputs with ids a through d. loadQuizz() rewrites their labels from the current object. The radio ids line up with the answer keys, so checking a submission is a single comparison:

if (answer === quizData[currentQuizz].correct) score++

Because of this, you extend the quiz by appending an object, not by copy-pasting another block of HTML with its own handlers. The same approach works for any list-shaped interface.

Adding questions#

Append to quizData in script.js. Nothing else needs to change. The markup, the scoring and the end screen stay as they are, and the end screen reads its total from quizData.length.

Known limitations#

  • No feedback per answer. You find out the score at the end and never learn which ones you got wrong.
  • No going back. currentQuizz only increments.
  • The end screen replaces the container with innerHTML and restarts via location.reload() instead of resetting state. It works, but it throws away the page.
  • alert() for the empty-answer case. Fine for a demo; an inline message would not block the thread or look like a browser error.

Running locally#

git clone https://github.com/Vette1123/Quizz-App.git
cd Quizz-App
# open index.html — no build, no server needed

Built by Mohamed Gado

More projects

  • Cool Sidebar Navigation2021 · JavaScript
  • Doggo2021 · JavaScript
  • Strong Password Generator2021 · JavaScript
All projects