0
0
Svelteframework~20 mins

Writable stores in Svelte - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Writable Store Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
state_output
intermediate
2: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);
A10
B5
C0
D15
Attempts:
2 left
💡 Hint
Remember that each update uses the current store value as input.
component_behavior
intermediate
2: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>
AClicks: 1
BClicks: 0
CClicks: 2
DClicks: undefined
Attempts:
2 left
💡 Hint
The $ prefix subscribes to the store and updates the UI automatically.
📝 Syntax
advanced
2: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?
A
import { writable } from 'svelte/store';

function loggerStore(initial) {
  const { subscribe, set, update } = writable(initial);
  return {
    subscribe,
    set(value) {
      console.log('Setting:', value);
      update(value);
    },
    update
  };
}
B
import { writable } from 'svelte/store';

function loggerStore(initial) {
  const { subscribe, set, update } = writable(initial);
  return {
    subscribe,
    set(value) {
      console.log('Setting:', value);
      return set(value);
    },
    update
  };
}
C
import { writable } from 'svelte/store';

function loggerStore(initial) {
  const { subscribe, set, update } = writable(initial);
  return {
    subscribe,
    set(value) {
      console.log('Setting:', value);
      this.set(value);
    },
    update
  };
}
D
import { writable } from 'svelte/store';

function loggerStore(initial) {
  const { subscribe, set, update } = writable(initial);
  return {
    subscribe,
    set: (value) =&gt; {
      console.log('Setting:', value);
      set(value);
    },
    update
  };
}
Attempts:
2 left
💡 Hint
The custom set method must call the original set function to update the store.
🔧 Debug
advanced
2: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>
AThe store is not updated correctly because update function is missing a return statement.
BThe subscription does not trigger reactive updates because 'value' is not a reactive variable.
CThe subscribe callback is never called because the store is not initialized properly.
DThe button click handler is not connected to the increment function.
Attempts:
2 left
💡 Hint
In Svelte, variables assigned inside subscribe callbacks do not trigger UI updates unless reactive.
🧠 Conceptual
expert
3: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?
ABoth components see the same store value and updates from one component reflect immediately in the other.
BEach component has its own copy of the store, so updates do not affect the other component.
CUpdates in one component cause errors in the other due to conflicting subscriptions.
DThe store resets to its initial value whenever a component updates it.
Attempts:
2 left
💡 Hint
Writable stores are singletons when imported from the same module.