0
0
Svelteframework~20 mins

Derived stores in Svelte - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Derived Store Master
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 Svelte derived store example?

Consider the following Svelte code using derived stores:

import { writable, derived } from 'svelte/store';

const count = writable(2);
const double = derived(count, $count => $count * 2);

let output = '';
double.subscribe(value => {
  output = `Value is ${value}`;
});

count.set(5);

What is the final value of output after count.set(5)?

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

const count = writable(2);
const double = derived(count, $count => $count * 2);

let output = '';
double.subscribe(value => {
  output = `Value is ${value}`;
});

count.set(5);
A"Value is 10"
B"Value is 2"
C"Value is 4"
D"Value is 5"
Attempts:
2 left
💡 Hint

Remember that the derived store updates when the original store changes.

state_output
intermediate
2:00remaining
What is the value of sum after these updates?

Given these Svelte stores:

import { writable, derived } from 'svelte/store';

const a = writable(3);
const b = writable(7);
const sum = derived([a, b], ([$a, $b]) => $a + $b);

let currentSum;
sum.subscribe(value => currentSum = value);

a.set(5);
b.set(10);

What is the final value of currentSum?

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

const a = writable(3);
const b = writable(7);
const sum = derived([a, b], ([$a, $b]) => $a + $b);

let currentSum;
sum.subscribe(value => currentSum = value);

a.set(5);
b.set(10);
A5
B15
C7
D10
Attempts:
2 left
💡 Hint

Both a and b change before you check currentSum.

📝 Syntax
advanced
2:00remaining
Which option correctly creates a derived store from two writable stores?

Choose the correct syntax to create a derived store fullName from two writable stores firstName and lastName that concatenates them with a space.

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

const firstName = writable('John');
const lastName = writable('Doe');

// Which option correctly defines fullName?
Aconst fullName = derived([firstName, lastName], ($first, $last) => `${$first} ${$last}`);
Bconst fullName = derived(firstName, lastName, ($first, $last) => `${$first} ${$last}`);
Cconst fullName = derived([firstName, lastName], ([$first, $last]) => `${$first} ${$last}`);
Dconst fullName = derived(firstName, ($first) => `${$first} ${lastName}`);
Attempts:
2 left
💡 Hint

Derived stores from multiple stores use an array as the first argument and a function with an array destructuring as the second.

🔧 Debug
advanced
2:00remaining
Why does this derived store not update as expected?

Look at this Svelte code:

import { writable, derived } from 'svelte/store';

const count = writable(1);
const double = derived(count, $count => {
  count.set($count * 2);
  return $count * 2;
});

double.subscribe(value => console.log(value));
count.set(3);

What problem does this code cause?

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

const count = writable(1);
const double = derived(count, $count => {
  count.set($count * 2);
  return $count * 2;
});

double.subscribe(value => console.log(value));
count.set(3);
AIt causes an infinite loop because the derived store sets the original store inside its callback.
BIt works fine and logs 6 once.
CIt throws a syntax error due to invalid callback.
DIt logs 3 and then stops without updating.
Attempts:
2 left
💡 Hint

Think about what happens when a store updates inside its own derived callback.

🧠 Conceptual
expert
2:00remaining
How many times does the derived store callback run in this scenario?

Given these stores:

import { writable, derived } from 'svelte/store';

const x = writable(1);
const y = writable(2);

const sum = derived([x, y], ([$x, $y], { set }) => {
  set($x + $y);
});

let count = 0;
sum.subscribe(() => count++);

x.set(3);
y.set(4);
x.set(5);

How many times is the derived store callback executed?

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

const x = writable(1);
const y = writable(2);

const sum = derived([x, y], ([$x, $y], { set }) => {
  set($x + $y);
});

let count = 0;
sum.subscribe(() => count++);

x.set(3);
y.set(4);
x.set(5);
A2
B3
C5
D4
Attempts:
2 left
💡 Hint

Remember the derived store runs once initially and then on every change of any dependency.