0
0
Svelteframework~20 mins

Custom store logic in Svelte - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Custom Store Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this custom store subscription?

Consider this Svelte custom store that counts up every second. What will be logged to the console after 3 seconds?

Svelte
import { readable } from 'svelte/store';

const counter = readable(0, function start(set) {
  let count = 0;
  const interval = setInterval(() => {
    count += 1;
    set(count);
  }, 1000);
  return () => clearInterval(interval);
});

counter.subscribe(value => console.log(value));
A0, 1, 2, 3
B1, 2, 3
C1, 2, 3, 4
D0, 1, 2
Attempts:
2 left
💡 Hint

The subscribe callback is fired immediately with the initial value 0, then after each set call.

state_output
intermediate
2:00remaining
What is the value of the custom writable store after these updates?

Given this custom writable store, what is the final value after the code runs?

Svelte
import { writable } from 'svelte/store';

function createCounter() {
  const { subscribe, set, update } = writable(0);
  return {
    subscribe,
    increment: () => update(n => n + 1),
    reset: () => set(0)
  };
}

const counter = createCounter();
counter.increment();
counter.increment();
counter.reset();
counter.increment();
A0
B2
C3
D1
Attempts:
2 left
💡 Hint

Track each method call and its effect on the store value.

📝 Syntax
advanced
2:00remaining
Which option correctly implements a custom store with a derived value?

Which code snippet correctly creates a custom store that holds a number and a derived store that doubles it?

A
import { writable, derived } from 'svelte/store';
const num = writable(5);
const double = derived(num, num => num * 2);
export { num, double };
B
import { writable, derived } from 'svelte/store';
const num = writable(5);
const double = derived(num, $num => $num * 2);
export { num, double };
C
import { writable, derived } from 'svelte/store';
const num = writable(5);
const double = derived(num, ($num) => { return $num * 2; });
export { num, double };
D
import { writable, derived } from 'svelte/store';
const num = writable(5);
const double = derived(num, (value) => value * 2);
export { num, double };
Attempts:
2 left
💡 Hint

Remember the derived store callback receives the store value as the first argument, often named with a $ prefix in examples.

🔧 Debug
advanced
2:00remaining
Why does this custom store cause a memory leak?

Examine this custom store code. Why might it cause a memory leak when used in a Svelte component?

Svelte
import { readable } from 'svelte/store';

function createTimer() {
  let count = 0;
  const interval = setInterval(() => {
    count += 1;
  }, 1000);
  return readable(count, set => {
    set(count);
    return () => clearInterval(interval);
  });
}

const timer = createTimer();
AThe interval starts immediately and is never cleared because the cleanup function is returned inside the readable constructor but the interval is created outside the start function.
BThe set function is called only once, so the store never updates.
CThe count variable is not reactive, so the store value never changes.
DThe readable store requires a writable store to update values.
Attempts:
2 left
💡 Hint

Consider when the interval starts and when the cleanup function runs.

🧠 Conceptual
expert
2:00remaining
What is the main benefit of using a custom store with start and stop logic in Svelte?

Why would you create a custom store with explicit start and stop functions instead of a simple writable store?

ATo automatically persist store data to local storage without extra code.
BTo allow multiple components to write to the store simultaneously without conflicts.
CTo manage resource-intensive processes only while there are active subscribers, improving performance and avoiding unnecessary work.
DTo enable two-way binding with form inputs without using Svelte's bind directive.
Attempts:
2 left
💡 Hint

Think about when you want to start or stop background tasks based on usage.