0
0
Svelteframework~20 mins

Reactive declarations (let) 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 component code:

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

  <button on:click={() => count++}>Increment</button>
  <p>Double: {double}</p>

What will be shown in the paragraph after clicking the button 3 times?

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

<button on:click={() => count++}>Increment</button>
<p>Double: {double}</p>
ADouble: 8
BDouble: 6
CDouble: 4
DDouble: 2
Attempts:
2 left
💡 Hint

Remember that $: runs whenever count changes.

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

Given this Svelte script:

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

What is the final value of sum?

Svelte
<script>
  let a = 2;
  let b = 3;
  $: sum = a + b;
  a = 5;
</script>
A5
B8
C2
D3
Attempts:
2 left
💡 Hint

Reactive declarations update when their dependencies change.

📝 Syntax
advanced
1:30remaining
Which option correctly declares a reactive statement with let in Svelte?

Choose the valid syntax for a reactive declaration that updates total when x or y change.

A$: total = x + y;
Blet $: total = x + y;
C$: let total = x + y;
Dlet total $: = x + y;
Attempts:
2 left
💡 Hint

Reactive declarations use $: before the statement without let.

🔧 Debug
advanced
2:30remaining
Why does this reactive declaration not update as expected?

Look at this Svelte code:

  <script>
    let count = 0;
    $: double = count * 2;

    function increment() {
      count += 1;
      double = count * 3;
    }
  </script>

  <button on:click={increment}>Click</button>
  <p>Double: {double}</p>

Why does the paragraph show double as count * 2 instead of count * 3 after clicking?

Svelte
<script>
  let count = 0;
  $: double = count * 2;

  function increment() {
    count += 1;
    double = count * 3;
  }
</script>

<button on:click={increment}>Click</button>
<p>Double: {double}</p>
AThe variable double is not declared with let.
BThe function increment does not update count correctly.
CSvelte does not allow assignments inside functions.
DThe reactive declaration overwrites the manual assignment to double after increment runs.
Attempts:
2 left
💡 Hint

Think about how reactive declarations run after any change.

🧠 Conceptual
expert
3:00remaining
What happens if a reactive declaration depends on itself in Svelte?

Consider this code snippet:

  <script>
    let a = 1;
    $: a = a + 1;
  </script>

What will happen when this component runs?

Svelte
<script>
  let a = 1;
  $: a = a + 1;
</script>
ASvelte will throw a compile-time error.
BThe value of a will be 2 and stop updating.
CThe component will enter an infinite loop and crash.
DThe reactive declaration will be ignored.
Attempts:
2 left
💡 Hint

Think about what happens when a reactive statement changes the variable it depends on.