0
0
Svelteframework~20 mins

Immutable patterns for updates in Svelte - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Immutable Update Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output after updating the array immutably?

Consider this Svelte component code that updates a list immutably. What will be the rendered list after clicking the button once?

Svelte
  <script>
    let items = ['apple', 'banana', 'cherry'];
    function addItem() {
      items = [...items, 'date'];
    }
  </script>

  <button on:click={addItem}>Add Date</button>
  <ul>
    {#each items as item}
      <li>{item}</li>
    {/each}
  </ul>
A<ul><li>apple</li><li>banana</li><li>cherry</li></ul>
B<ul><li>date</li></ul>
C<ul><li>apple</li><li>banana</li><li>cherry</li><li>date</li></ul>
D<ul><li>apple</li><li>banana</li><li>date</li></ul>
Attempts:
2 left
💡 Hint

Think about how the spread operator creates a new array including the old items plus the new one.

state_output
intermediate
2:00remaining
What is the value of the object after immutable update?

Given this Svelte code, what is the value of user after calling updateName()?

Svelte
  <script>
    let user = { name: 'Alice', age: 30 };
    function updateName() {
      user = { ...user, name: 'Bob' };
    }
    updateName();
  </script>
A{ name: 'Bob', age: 30 }
B{ name: 'Bob' }
C{ name: 'Alice', age: 30 }
D{ age: 30 }
Attempts:
2 left
💡 Hint

Remember the spread operator copies all properties, then the new name replaces the old one.

🔧 Debug
advanced
2:00remaining
Why does this immutable update not trigger reactivity?

Look at this Svelte code snippet. Why does the UI not update after calling updateCount()?

Svelte
  <script>
    let state = { count: 0 };
    function updateCount() {
      state.count += 1;
    }
  </script>

  <button on:click={updateCount}>Increment</button>
  <p>{state.count}</p>
ABecause Svelte requires a special method to update nested properties.
BBecause the 'count' property is not a reactive variable.
CBecause the function 'updateCount' is not called correctly.
DBecause the object reference 'state' does not change, Svelte does not detect the update.
Attempts:
2 left
💡 Hint

Think about how Svelte tracks changes to variables by their references.

📝 Syntax
advanced
2:00remaining
Which option correctly updates a nested array immutably in Svelte?

Given this state, which code correctly updates the second item in colors immutably?

Svelte
  <script>
    let palette = { colors: ['red', 'green', 'blue'] };
    function updateGreen() {
      // update 'green' to 'lime'
    }
  </script>
Apalette = { ...palette, colors: [...palette.colors.slice(0,1), 'lime', ...palette.colors.slice(2)] };
Bpalette.colors[1] = 'lime';
Cpalette = { ...palette, colors: palette.colors.splice(1, 1, 'lime') };
Dpalette = { colors: ['red', 'lime', 'blue'] };
Attempts:
2 left
💡 Hint

Remember to create new arrays and objects instead of mutating existing ones.

🧠 Conceptual
expert
2:00remaining
Why is immutability important for Svelte reactivity?

Which statement best explains why immutable updates are crucial for Svelte's reactivity system?

ASvelte automatically detects deep mutations inside objects without new assignments.
BSvelte tracks changes by variable references, so immutable updates create new references triggering reactivity.
CImmutability prevents any changes to variables, ensuring UI never updates unexpectedly.
DSvelte requires all variables to be declared as constants to work properly.
Attempts:
2 left
💡 Hint

Think about how Svelte knows when to update the UI.