0
0
Svelteframework~20 mins

Readable stores in Svelte - Practice Problems & Coding Challenges

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

Consider this Svelte component using a readable store:

import { readable } from 'svelte/store';

const time = readable(new Date(), function start(set) {
  const interval = setInterval(() => {
    set(new Date());
  }, 1000);

  return () => clearInterval(interval);
});

<script>
  import { time } from './timeStore';
</script>

<p>Current time: {$time.toLocaleTimeString()}</p>

What will the component display?

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

const time = readable(new Date(), function start(set) {
  const interval = setInterval(() => {
    set(new Date());
  }, 1000);

  return () => clearInterval(interval);
});

<script>
  import { time } from './timeStore';
</script>

<p>Current time: {$time.toLocaleTimeString()}</p>
AThe current time updates every second, showing the live clock.
BThe time updates only once after 1 second and then stops.
CThe component throws an error because readable stores cannot be used in markup.
DThe time is fixed at the moment the component loads and never updates.
Attempts:
2 left
💡 Hint

Think about what the setInterval inside the readable store does.

state_output
intermediate
1:30remaining
What is the value of the store after 3 seconds?

Given this readable store in Svelte:

import { readable } from 'svelte/store';

const count = readable(0, (set) => {
  let i = 0;
  const interval = setInterval(() => {
    i += 1;
    set(i);
  }, 1000);

  return () => clearInterval(interval);
});

What will be the value of $count after 3 seconds?

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

const count = readable(0, (set) => {
  let i = 0;
  const interval = setInterval(() => {
    i += 1;
    set(i);
  }, 1000);

  return () => clearInterval(interval);
});
A0
B3
C1
Dundefined
Attempts:
2 left
💡 Hint

Count how many times the interval callback runs in 3 seconds.

📝 Syntax
advanced
2:00remaining
Which option correctly creates a readable store with cleanup?

Which of the following code snippets correctly creates a readable store that updates every second and cleans up properly?

A
const timer = readable(0, (set) =&gt; {
  setInterval(() =&gt; set(Date.now()), 1000);
  return clearInterval;
});
B
const timer = readable(0, () =&gt; {
  setInterval(() =&gt; set(Date.now()), 1000);
});
C
const timer = readable(0, (set) =&gt; {
  const id = setInterval(() =&gt; set(Date.now()), 1000);
  clearInterval(id);
});
D
const timer = readable(0, set =&gt; {
  const id = setInterval(() =&gt; set(Date.now()), 1000);
  return () =&gt; clearInterval(id);
});
Attempts:
2 left
💡 Hint

Remember the cleanup function must be returned and clear the interval.

🔧 Debug
advanced
2:30remaining
Why does this readable store not update the component?

Look at this Svelte readable store and component:

const data = readable('initial', (set) => {
  fetch('/api/data')
    .then(response => response.json())
    .then(json => {
      set(json.value);
    });
});

<script>
  import { data } from './dataStore';
</script>

<p>Data: {$data}</p>

The component always shows 'initial' and never updates. Why?

Svelte
const data = readable('initial', (set) => {
  fetch('/api/data')
    .then(response => response.json())
    .then(json => {
      set(json.value);
    });
});

<script>
  import { data } from './dataStore';
</script>

<p>Data: {$data}</p>
AThe readable store must call set synchronously, async calls are not allowed.
BThe fetch call is incorrect and never resolves, so set is never called.
CThe fetch promise runs but the store subscriber is never activated, so set has no effect.
DThe component does not subscribe to the store correctly, so it never updates.
Attempts:
2 left
💡 Hint

Think about when the start function in readable runs and when subscribers exist.

🧠 Conceptual
expert
3:00remaining
How does a Svelte readable store differ from a writable store in usage?

Which statement best describes the difference between a readable store and a writable store in Svelte?

AA readable store allows only internal updates via its start function; a writable store allows external updates via its set and update methods.
BA readable store can be updated externally by calling set; a writable store cannot be updated after creation.
CA readable store requires manual subscription; a writable store automatically updates all components without subscription.
DA readable store is immutable and never changes; a writable store changes only once.
Attempts:
2 left
💡 Hint

Consider who can change the store's value after creation.