0
0
Svelteframework~20 mins

Derived values with $: in Svelte - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Svelte Reactivity 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 using a derived value with $:

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

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

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

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

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

Remember the initial count and how many times it increments.

state_output
intermediate
2:00remaining
What is the value of 'fullName' after updating 'lastName'?

Given this Svelte code:

  <script>
    let firstName = 'Jane';
    let lastName = 'Doe';
    $: fullName = `${firstName} ${lastName}`;
  </script>

  <button on:click={() => lastName = 'Smith'}>Change Last Name</button>
  <p>Full Name: {fullName}</p>

What will be displayed after clicking the button once?

Svelte
  <script>
    let firstName = 'Jane';
    let lastName = 'Doe';
    $: fullName = `${firstName} ${lastName}`;
  </script>

  <button on:click={() => lastName = 'Smith'}>Change Last Name</button>
  <p>Full Name: {fullName}</p>
AFull Name: Jane Doe
BFull Name: undefined Smith
CFull Name: Jane Smith
DFull Name: Smith Jane
Attempts:
2 left
💡 Hint

Derived values update automatically when dependencies change.

📝 Syntax
advanced
1:30remaining
Which option correctly declares a derived value with $: in Svelte?

Which of these code snippets correctly uses the $: syntax to create a derived value?

Alet $: total = price + tax;
B$: total = price + tax;
C$ total = price + tax;
Dtotal $: = price + tax;
Attempts:
2 left
💡 Hint

The $: label is used before the statement without let.

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

Look at this Svelte code:

  <script>
    let a = 1;
    let b = 2;
    $: sum = a + b;

    function updateA() {
      a = a + 1;
    }
  </script>

  <button on:click={updateA}>Update A</button>
  <p>Sum: {sum}</p>

After clicking the button, the sum does not update. What is the likely cause?

Svelte
  <script>
    let a = 1;
    let b = 2;
    $: sum = a + b;

    function updateA() {
      a = a + 1;
    }
  </script>

  <button on:click={updateA}>Update A</button>
  <p>Sum: {sum}</p>
AThere is no issue; the sum updates correctly when 'a' changes.
BThe function 'updateA' is not called correctly in the button's on:click.
CThe derived value 'sum' is not reactive because 'b' never changes.
DThe variable 'a' is not reactive because it is not declared with 'let'.
Attempts:
2 left
💡 Hint

Check how Svelte tracks reactive variables and updates.

🧠 Conceptual
expert
2:30remaining
How does Svelte's $: reactive statement differ from React's useMemo?

Which statement best describes the difference between Svelte's $: reactive statements and React's useMemo hook?

A<code>$:</code> automatically tracks dependencies without explicit listing, while <code>useMemo</code> requires dependency arrays.
B<code>$:</code> runs only once on mount, <code>useMemo</code> runs on every render.
C<code>$:</code> is used only for side effects, <code>useMemo</code> caches values.
D<code>$:</code> requires manual dependency tracking, <code>useMemo</code> tracks dependencies automatically.
Attempts:
2 left
💡 Hint

Think about how dependencies are declared and tracked in both.