stateDiagram-v2
direction LR
[*] --> Asking
Asking --> Answered: click a choice
Answered --> Asking: Next (more questions)
Answered --> Finished: Next (last question)
Finished --> Asking: Restart
Week 4 - Building the Quiz App
This week we’ll put our Svelte skills into practice by building a quiz app, bringing together components, state, and styling in a larger interactive project.
Svelte Playground: Week 4 - Quiz App
Code Examples
<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>
<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><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}<script>
const { label, style, onclick } = $props();
</script>
<button class="btn btn-block {style}" {onclick}>{label}</button><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>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 }
];State Diagrams
The quiz as a whole:
A single 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
Webinar Recording
The webinar recording will be available during the following week.
