Bird
Raised Fist0
Svelteframework~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does the subscribe method of a Svelte store do?
easy
A. It resets the store to its initial value.
B. It updates the store value directly.
C. It deletes the store permanently.
D. It lets you watch store changes and react immediately.

Solution

  1. Step 1: Understand the purpose of subscribe

    The subscribe method allows you to listen to changes in the store's value.
  2. Step 2: Identify what subscribe enables

    It lets your code react immediately whenever the store value changes.
  3. Final Answer:

    It lets you watch store changes and react immediately. -> Option D
  4. Quick Check:

    subscribe watches changes = C [OK]
Hint: Remember: subscribe means listen and react to changes [OK]
Common Mistakes:
  • Thinking subscribe changes the store value
  • Confusing subscribe with reset or delete
  • Assuming subscribe returns the store value directly
2. Which of the following is the correct syntax to subscribe to a Svelte store named count?
easy
A. count.subscribe = value => console.log(value);
B. count.subscribe(value => console.log(value));
C. subscribe(count, value => console.log(value));
D. count.onSubscribe(value => console.log(value));

Solution

  1. Step 1: Recall the store subscription syntax

    The correct way is to call subscribe as a method on the store with a callback.
  2. Step 2: Match the syntax to the options

    count.subscribe(value => console.log(value)); correctly calls count.subscribe with a function to receive updates.
  3. Final Answer:

    count.subscribe(value => console.log(value)); -> Option B
  4. Quick Check:

    Method call subscribe with callback = A [OK]
Hint: subscribe is a method: store.subscribe(callback) [OK]
Common Mistakes:
  • Assigning subscribe instead of calling it
  • Using a non-existent method like onSubscribe
  • Calling subscribe as a standalone function
3. Given the code below, what will be logged to the console?
import { writable } from 'svelte/store';
const count = writable(0);
const unsubscribe = count.subscribe(value => console.log(value));
count.set(5);
unsubscribe();
count.set(10);
medium
A. 0, 5
B. 0, 5, 10
C. 5, 10
D. 10

Solution

  1. Step 1: Understand initial subscription behavior

    When subscribing, the callback immediately receives the current value, which is 0.
  2. Step 2: Trace subsequent updates and unsubscription

    After setting to 5, the callback logs 5. Then unsubscribe() stops listening, so setting to 10 logs nothing.
  3. Final Answer:

    0, 5 -> Option A
  4. Quick Check:

    subscribe logs initial + updates until unsubscribe = B [OK]
Hint: subscribe logs current value immediately, stops after unsubscribe [OK]
Common Mistakes:
  • Expecting logs after unsubscribe
  • Missing initial immediate callback call
  • Confusing set with update method
4. What is wrong with this code snippet?
import { writable } from 'svelte/store';
const count = writable(0);
count.subscribe(value => console.log(value));
count.unsubscribe();
medium
A. You must import subscribe separately from the store.
B. The subscribe callback must return a value.
C. You cannot call unsubscribe directly on the store.
D. The store must be initialized with a string, not a number.

Solution

  1. Step 1: Understand unsubscribe usage

    The subscribe method returns an unsubscribe function to stop listening.
  2. Step 2: Identify the error in calling unsubscribe

    Calling unsubscribe directly on the store is invalid; you must call the function returned by subscribe.
  3. Final Answer:

    You cannot call unsubscribe directly on the store. -> Option C
  4. Quick Check:

    unsubscribe is returned by subscribe, not a store method [OK]
Hint: unsubscribe comes from subscribe, not store itself [OK]
Common Mistakes:
  • Calling unsubscribe on the store instead of returned function
  • Expecting subscribe callback to return a value
  • Confusing store initialization types
5. You want to create a custom store that logs every subscription and unsubscription. Which of the following correctly implements the subscribe method to follow the store contract?
hard
A. function customStore() { let value = 0; return { subscribe(run) { console.log('subscribed'); run(value); return () => console.log('unsubscribed'); } }; }
B. function customStore() { let value = 0; return { subscribe(run) { console.log('subscribed'); return () => console.log('unsubscribed'); } }; }
C. function customStore() { let value = 0; return { subscribe() { console.log('subscribed'); return () => console.log('unsubscribed'); } }; }
D. function customStore() { let value = 0; return { subscribe(run) { run(value); console.log('subscribed'); } }; }

Solution

  1. Step 1: Check the subscribe method signature

    The subscribe method must accept a callback run and call it immediately with the current value.
  2. Step 2: Verify logging and return of unsubscribe

    function customStore() { let value = 0; return { subscribe(run) { console.log('subscribed'); run(value); return () => console.log('unsubscribed'); } }; } logs 'subscribed' before calling run(value) and returns a function that logs 'unsubscribed'.
  3. Step 3: Confirm distractors are invalid

    B misses calling run(value), C lacks run parameter, D misses returning the unsubscribe function.
  4. Final Answer:

    correctly implements subscribe with logging and unsubscribe function -> Option A
  5. Quick Check:

    subscribe calls run(value) and returns unsubscribe function [OK]
Hint: subscribe calls run(value) then returns unsubscribe function [OK]
Common Mistakes:
  • Not calling run with current value
  • Missing the unsubscribe function return
  • Logging after calling run instead of before
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard