0
0
Svelteframework~20 mins

Store contract (subscribe method) in Svelte - Practice Problems & Coding Challenges

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

Consider this Svelte store and subscription code snippet. What will be logged to the console?

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

const count = writable(0);

const unsubscribe = count.subscribe(value => {
  console.log('Count is', value);
});

count.set(5);
unsubscribe();
count.set(10);
ACount is 5\nCount is 10
BCount is 0\nCount is 5\nCount is 10
CCount is 0\nCount is 5
DCount is 0
Attempts:
2 left
💡 Hint

Remember what happens when you call the unsubscribe function.

📝 Syntax
intermediate
2:00remaining
Which option correctly implements a custom store with subscribe method?

Which of the following custom store implementations correctly follows the Svelte store contract by providing a subscribe method?

Afunction createStore() { let value = 0; return { subscribe: (run) => { run(value); return () => {}; } }; }
Bfunction createStore() { let value = 0; return { subscribe(run) { run(value); } }; }
Cfunction createStore() { let value = 0; return { subscribe: (run) => { run(value); } }; }
Dfunction createStore() { let value = 0; return { subscribe: (run) => { run(value); return null; } }; }
Attempts:
2 left
💡 Hint

The subscribe method must return an unsubscribe function.

🔧 Debug
advanced
2:00remaining
Why does this Svelte store subscription cause a runtime error?

Examine the code below. Why does it cause a runtime error when subscribing?

Svelte
function brokenStore() {
  let value = 0;
  return {
    subscribe: (run) => {
      run(value);
      return null;
    }
  };
}

const store = brokenStore();
const unsub = store.subscribe(val => console.log(val));
unsub();
ABecause subscribe is missing, store.subscribe is undefined.
BBecause run is not called inside subscribe, no value is logged.
CBecause value is not initialized, run(value) throws a ReferenceError.
DBecause subscribe returns null instead of a function, calling unsub() causes a TypeError.
Attempts:
2 left
💡 Hint

Check what the unsubscribe variable holds after subscribing.

state_output
advanced
2:00remaining
What is the final logged output after multiple subscriptions and updates?

Given this Svelte writable store and subscriptions, what is the console output?

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

const num = writable(1);

const unsub1 = num.subscribe(v => console.log('Sub1:', v));
num.set(2);
const unsub2 = num.subscribe(v => console.log('Sub2:', v));
num.set(3);
unsub1();
num.set(4);
unsub2();
num.set(5);
ASub1: 1\nSub1: 2\nSub2: 2\nSub1: 3\nSub2: 3\nSub2: 4
BSub1: 1\nSub1: 2\nSub2: 2\nSub1: 3\nSub2: 3\nSub1: 4
CSub1: 1\nSub1: 2\nSub2: 2\nSub1: 3\nSub2: 3\nSub1: 4\nSub2: 4
DSub1: 1\nSub1: 2\nSub2: 2\nSub1: 3\nSub2: 3\nSub1: 4\nSub2: 4\nSub2: 5
Attempts:
2 left
💡 Hint

Remember which subscriptions are active after each unsubscribe call.

🧠 Conceptual
expert
2:00remaining
What is the essential purpose of the subscribe method in Svelte stores?

Choose the best description of the subscribe method's role in Svelte stores.

AIt updates the store's value and notifies all subscribers.
BIt registers a callback to run whenever the store's value changes and returns a function to stop listening.
CIt immediately returns the current store value without registering any callback.
DIt creates a new store instance with an initial value.
Attempts:
2 left
💡 Hint

Think about how components react to store changes.