0
0
Svelteframework~20 mins

Reactive blocks for side effects in Svelte - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Reactive Blocks Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
1:30remaining
What happens when a reactive block updates a variable?

Consider this Svelte code snippet:

let count = 0;

$: doubled = count * 2;

function increment() {
  count += 1;
}

What is the value of doubled after calling increment() twice?

Svelte
let count = 0;

$: doubled = count * 2;

function increment() {
  count += 1;
}

increment();
increment();

// What is doubled now?
A0
B2
CUndefined
D4
Attempts:
2 left
💡 Hint

Reactive blocks run whenever their dependencies change.

lifecycle
intermediate
1:30remaining
When does a reactive block run in Svelte?

Given this Svelte code:

let a = 1;
let b = 2;

$: sum = a + b;

Which action will cause the reactive block to run?

Svelte
let a = 1;
let b = 2;

$: sum = a + b;

// Which of these triggers the reactive block?
// 1) a = 3;
// 2) b = 5;
// 3) sum = 10;
// 4) console.log(sum);
AAll 1, 2, 3, and 4
BOnly 1 and 2
COnly 4
DOnly 3
Attempts:
2 left
💡 Hint

Reactive blocks run when their dependencies change.

📝 Syntax
advanced
1:30remaining
Identify the syntax error in this reactive block

What is wrong with this Svelte reactive block?

let x = 5;

$: y = x * 2

$: z = y + 3;
Svelte
let x = 5;

$: y = x * 2

$: z = y + 3;
AReactive blocks must use curly braces
BReactive blocks cannot be declared consecutively
CNo error, code is valid
DMissing semicolon after reactive statement for y
Attempts:
2 left
💡 Hint

Check if semicolons are mandatory in Svelte reactive blocks.

🔧 Debug
advanced
2:00remaining
Why does this reactive block not update as expected?

Consider this Svelte code:

let items = [1, 2, 3];

$: total = items.reduce((a, b) => a + b, 0);

function addItem(num) {
  items.push(num);
}

After calling addItem(4), why does total not update?

Svelte
let items = [1, 2, 3];

$: total = items.reduce((a, b) => a + b, 0);

function addItem(num) {
  items.push(num);
}

addItem(4);

// What is total now?
ABecause items array was mutated directly, Svelte did not detect the change
BBecause reduce does not work on arrays
CBecause total is not reactive
DBecause addItem does not update items
Attempts:
2 left
💡 Hint

Think about how Svelte tracks changes in arrays.

🧠 Conceptual
expert
2:30remaining
How to avoid infinite loops with reactive blocks?

Given this Svelte code:

let a = 1;

$: a = a + 1;

What happens when this code runs, and how can you fix it?

Svelte
let a = 1;

$: a = a + 1;
AIt causes an infinite loop because the reactive block updates 'a' which triggers itself; fix by removing the reactive assignment
BIt runs once and stops; no fix needed
CIt throws a syntax error; fix by adding a semicolon
DIt updates 'a' to 2 and stops; fix by adding a condition
Attempts:
2 left
💡 Hint

Think about what triggers reactive blocks and what happens when they update their own dependencies.