Challenge - 5 Problems
Writable Store Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ state_output
intermediate2:00remaining
What is the output of this writable store update?
Consider this Svelte writable store code. What will be the final value of
count after the update?Svelte
import { writable } from 'svelte/store'; const count = writable(0); count.update(n => n + 5); count.update(n => n * 2); let finalValue; count.subscribe(value => finalValue = value);
Attempts:
2 left
💡 Hint
Remember that each update uses the current store value as input.
✗ Incorrect
The store starts at 0. First update adds 5, so value is 5. Second update multiplies by 2, so final value is 10.
❓ component_behavior
intermediate2:00remaining
How does this Svelte component react to writable store changes?
Given this Svelte component using a writable store, what will be displayed after clicking the button twice?
Svelte
<script> import { writable } from 'svelte/store'; const clicks = writable(0); function increment() { clicks.update(n => n + 1); } </script> <button on:click={increment}>Click me</button> <p>Clicks: {$clicks}</p>
Attempts:
2 left
💡 Hint
The $ prefix subscribes to the store and updates the UI automatically.
✗ Incorrect
Each click calls increment which adds 1 to the store. After two clicks, the value is 2 and displayed.
📝 Syntax
advanced2:30remaining
Which option correctly creates a writable store with a custom set method?
You want to create a writable store that logs every time its value changes. Which code snippet is correct?
Attempts:
2 left
💡 Hint
The custom set method must call the original set function to update the store.
✗ Incorrect
Option D correctly overrides set as an arrow function that logs and calls the original set. Option D uses a method shorthand but calls set recursively causing a stack overflow. Option D returns set's result which is undefined but not an error. Option D calls update with a value instead of a function, causing a runtime error.
🔧 Debug
advanced2:00remaining
Why does this writable store subscription not update the UI?
This Svelte component subscribes to a writable store but the UI does not update. What is the cause?
Svelte
<script> import { writable } from 'svelte/store'; const data = writable(0); let value; data.subscribe(val => { value = val; }); function increment() { data.update(n => n + 1); } </script> <button on:click={increment}>Increment</button> <p>Value: {value}</p>
Attempts:
2 left
💡 Hint
In Svelte, variables assigned inside subscribe callbacks do not trigger UI updates unless reactive.
✗ Incorrect
The variable 'value' is assigned inside subscribe but is not reactive. The UI does not update because Svelte does not track changes to plain variables outside reactive declarations or $store syntax.
🧠 Conceptual
expert3:00remaining
What happens when multiple components share and update the same writable store?
Two Svelte components import the same writable store and update it independently. What is the expected behavior?
Attempts:
2 left
💡 Hint
Writable stores are singletons when imported from the same module.
✗ Incorrect
Writable stores are shared instances. When multiple components import the same store, they share the same state. Updates in one component update the store and notify all subscribers, so other components see the change immediately.