0
0
Svelteframework~20 mins

Reactive statements ($:) in Svelte - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Svelte Reactive 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 component?

Consider this Svelte code snippet:

  <script>
    let count = 1;
    $: doubled = count * 2;
  </script>

  <button on:click={() => count++}>Increment</button>
  <p>Doubled value: {doubled}</p>

What will be displayed after clicking the button twice?

Svelte
<script>
  let count = 1;
  $: doubled = count * 2;
</script>

<button on:click={() => count++}>Increment</button>
<p>Doubled value: {doubled}</p>
ADoubled value: 2
BDoubled value: 4
CDoubled value: 3
DDoubled value: 6
Attempts:
2 left
💡 Hint

Remember the reactive statement updates whenever count changes.

state_output
intermediate
1:30remaining
What is the value of 'total' after this code runs?

Given this Svelte script:

<script>
  let a = 2;
  let b = 3;
  $: total = a + b;
  a = 5;
</script>

What is the value of total after these statements?

Svelte
<script>
  let a = 2;
  let b = 3;
  $: total = a + b;
  a = 5;
</script>
A2
B3
C8
D5
Attempts:
2 left
💡 Hint

Reactive statements run whenever their dependencies change.

📝 Syntax
advanced
1:30remaining
Which option causes a syntax error in Svelte reactive statements?

Identify the option that will cause a syntax error when used as a reactive statement in Svelte:

A$: doubled = count * 2 else doubled = 0;
B$: if (count &gt; 5) doubled = count * 2;
C$: { doubled = count * 2 }
D$: doubled = count * 2;
Attempts:
2 left
💡 Hint

Check the syntax of conditional expressions inside reactive statements.

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

Consider this Svelte code:

<script>
  let items = [];
  $: count = items.length;
  function addItem() {
    items.push('new');
  }
</script>

<button on:click={addItem}>Add</button>
<p>Count: {count}</p>

Clicking 'Add' does not update the count. Why?

Svelte
<script>
  let items = [];
  $: count = items.length;
  function addItem() {
    items.push('new');
  }
</script>

<button on:click={addItem}>Add</button>
<p>Count: {count}</p>
ABecause count is not declared with let.
BBecause modifying array with push does not trigger reactivity in Svelte.
CBecause the reactive statement is missing a dependency array.
DBecause the addItem function is not called on button click.
Attempts:
2 left
💡 Hint

Think about how Svelte tracks changes to arrays.

🧠 Conceptual
expert
2:30remaining
How does Svelte's reactive statement handle multiple dependencies?

Given this reactive statement:

$: result = a + b * c;

Which statement best describes when result updates?

A<code>result</code> updates whenever any of <code>a</code>, <code>b</code>, or <code>c</code> changes.
B<code>result</code> updates only when <code>b</code> or <code>c</code> changes.
C<code>result</code> updates only when <code>a</code> changes.
D<code>result</code> updates only when all of <code>a</code>, <code>b</code>, and <code>c</code> change simultaneously.
Attempts:
2 left
💡 Hint

Consider how reactive statements track dependencies automatically.