0
0
Svelteframework~20 mins

Keyed each blocks in Svelte - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Keyed Each Blocks Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output when using a keyed each block with dynamic list updates?

Consider the following Svelte component that uses a keyed each block to render a list of items. What will be the rendered output after clicking the button once?

Svelte
  <script>
    let items = [{ id: 1, name: 'Apple' }, { id: 2, name: 'Banana' }];
    function update() {
      items = [{ id: 2, name: 'Banana' }, { id: 1, name: 'Apple' }];
    }
  </script>

  <button on:click={update}>Swap Items</button>

  <ul>
    {#each items as item (item.id)}
      <li>{item.name}</li>
    {/each}
  </ul>
A<ul><li>Banana</li><li>Banana</li></ul>
B<ul><li>Banana</li><li>Apple</li></ul>
C<ul><li>Apple</li><li>Apple</li></ul>
D<ul><li>Apple</li><li>Banana</li></ul>
Attempts:
2 left
💡 Hint

Remember that keyed each blocks track items by their key and reorder DOM elements instead of recreating them.

state_output
intermediate
2:00remaining
What is the value of the variable after removing an item in a keyed each block?

Given the following Svelte code, what will be the value of items after clicking the 'Remove Banana' button?

Svelte
  <script>
    let items = [{ id: 1, name: 'Apple' }, { id: 2, name: 'Banana' }, { id: 3, name: 'Cherry' }];
    function removeBanana() {
      items = items.filter(item => item.id !== 2);
    }
  </script>

  <button on:click={removeBanana}>Remove Banana</button>

  <ul>
    {#each items as item (item.id)}
      <li>{item.name}</li>
    {/each}
  </ul>
A[{ id: 1, name: 'Apple' }, { id: 3, name: 'Cherry' }]
B[{ id: 2, name: 'Banana' }, { id: 3, name: 'Cherry' }]
C[{ id: 1, name: 'Apple' }, { id: 2, name: 'Banana' }]
D[{ id: 3, name: 'Cherry' }]
Attempts:
2 left
💡 Hint

Filtering removes the item with id === 2.

📝 Syntax
advanced
2:00remaining
Which option correctly uses a keyed each block in Svelte?

Identify the correct syntax for a keyed each block that iterates over users array keyed by user.id.

A{#each users as user, user.id}<p>{user.name}</p>{/each}
B{#each users (user.id) as user}<p>{user.name}</p>{/each}
C{#each users as user key user.id}<p>{user.name}</p>{/each}
D{#each users as user (user.id)}<p>{user.name}</p>{/each}
Attempts:
2 left
💡 Hint

The key expression goes in parentheses immediately after the as clause.

🔧 Debug
advanced
2:00remaining
Why does this keyed each block cause a runtime error?

Examine the code below. Why does it cause a runtime error when rendering?

Svelte
  <script>
    let items = [{ id: 1, name: 'A' }, { id: 2, name: 'B' }];
  </script>

  <ul>
    {#each items as item (item.key)}
      <li>{item.name}</li>
    {/each}
  </ul>
ABecause the key expression must be a string literal, not a property.
BBecause the items array is empty, so nothing renders.
CBecause 'item.key' is undefined; the key must be a valid property on each item.
DBecause the each block is missing a closing {/each} tag.
Attempts:
2 left
💡 Hint

Check the property used as the key in the each block.

🧠 Conceptual
expert
3:00remaining
How does Svelte's keyed each block optimize DOM updates compared to unkeyed each blocks?

Choose the best explanation of how Svelte uses keys in each blocks to optimize rendering.

ASvelte uses keys to track each item uniquely, so it can reorder or update existing DOM nodes instead of recreating them, improving performance.
BKeys in each blocks tell Svelte to always recreate all DOM nodes on every update to ensure freshness.
CSvelte ignores keys and always updates the DOM by replacing the entire list.
DKeys are only used for styling and have no effect on DOM updates.
Attempts:
2 left
💡 Hint

Think about how keys help identify items uniquely during list changes.