init to prod

Week 4 - Building the Quiz App

Sam Kamperis

Thursday, October 29, 2026

Daisy UI

  <link href="https://cdn.jsdelivr.net/npm/daisyui@5" rel="stylesheet" type="text/css" />
  <script src="https://cdn.jsdelivr.net/npm/@tailwindcss/browser@4"></script>
  <link href="https://cdn.jsdelivr.net/npm/daisyui@5/themes.css" rel="stylesheet" type="text/css" />

svelte:head element

<svelte:head>
  <link href="https://cdn.jsdelivr.net/npm/daisyui@5" rel="stylesheet" type="text/css" />
  <script src="https://cdn.jsdelivr.net/npm/@tailwindcss/browser@4"></script>
  <link href="https://cdn.jsdelivr.net/npm/daisyui@5/themes.css" rel="stylesheet" type="text/css" />
</svelte:head>

Quiz App Architecture

Four components and one data file:

  • questions.js - the questions (plain data)
  • App.svelte - owns all the state
  • Question.svelte - shows a question and its choices
  • Choice.svelte - a single answer button
  • Result.svelte - final score and restart

Data first

export const questions = [
  { text: 'Which keyword creates reactive state in Svelte 5?',
    choices: ['$state', 'let', 'var'], answer: 0 },
  { text: 'How does a child receive values?',
    choices: ['$props', '$derived', '$effect'], answer: 0 }
];

answer is the index of the correct choice.

State diagram: the quiz

stateDiagram-v2
    direction LR
    [*] --> Asking
    Asking --> Answered: click a choice
    Answered --> Asking: Next (more questions)
    Answered --> Finished: Next (last question)
    Finished --> Asking: Restart

State diagram: a choice button

stateDiagram-v2
    direction LR
    [*] --> Idle
    Idle --> Correct: answered, this is the answer
    Idle --> Wrong: answered, this was picked
    Correct --> Idle: Next
    Wrong --> Idle: Next

Choice.svelte

<script>
  const { label, style, onclick } = $props();
</script>

<button class="btn btn-block {style}" {onclick}>{label}</button>

daisyUI does the work: btn, btn-block, btn-success, btn-error.

Question.svelte

<script>
  import Choice from './Choice.svelte';
  const { question, selected, onanswer } = $props();

  function style(i) {
    if (selected === null) return '';
    if (i === question.answer) return 'btn-success';
    if (i === selected) return 'btn-error';
    return '';
  }
</script>

<h2 class="card-title">{question.text}</h2>
{#each question.choices as label, i}
  <Choice {label} style={style(i)} onclick={() => onanswer(i)} />
{/each}

Result.svelte

<script>
  const { score, total, onrestart } = $props();
</script>

<div class="stat">
  <div class="stat-title">Your score</div>
  <div class="stat-value">{score} / {total}</div>
</div>
<button class="btn btn-primary" onclick={onrestart}>Restart</button>

App.svelte (script)

<script>
  import { questions } from './questions.js';
  import Question from './Question.svelte';
  import Result from './Result.svelte';

  let current = $state(0);
  let score = $state(0);
  let selected = $state(null);
  let finished = $state(false);

  function answer(i) {
    if (selected !== null) return;
    selected = i;
    if (i === questions[current].answer) score++;
  }
  function next() {
    selected = null;
    if (current + 1 < questions.length) current++;
    else finished = true;
  }
  function restart() {
    current = 0; score = 0; selected = null; finished = false;
  }
</script>

App.svelte (markup)

<svelte:head>
  <link href="https://cdn.jsdelivr.net/npm/daisyui@5" rel="stylesheet" type="text/css" />
  <script src="https://cdn.jsdelivr.net/npm/@tailwindcss/browser@4"></script>
  <link href="https://cdn.jsdelivr.net/npm/daisyui@5/themes.css" rel="stylesheet" type="text/css" />
</svelte:head>

<main data-theme="cupcake" class="card card-body max-w-md mx-auto">
  <progress class="progress" value={finished ? questions.length : current} max={questions.length}></progress>
  {#if finished}
    <Result {score} total={questions.length} onrestart={restart} />
  {:else}
    <Question question={questions[current]} {selected} onanswer={answer} />
    {#if selected !== null}
      <button class="btn btn-primary" onclick={next}>Next</button>
    {/if}
  {/if}
</main>

Exercise

Add a timer per question, or a StartScreen.svelte component.

Thank you! Any questions?