Challenge - 5 Problems
Svelte Fragment Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What does this
svelte:fragment render?Consider this Svelte code snippet using
svelte:fragment. What will be rendered in the browser?Svelte
<script> let show = true; </script> <svelte:fragment> {#if show} <p>Hello</p> {/if} <p>World</p> </svelte:fragment>
Attempts:
2 left
💡 Hint
Remember that
svelte:fragment groups elements without adding extra HTML tags.✗ Incorrect
The svelte:fragment groups the {#if} block and the <p>World</p> paragraph together without adding extra wrappers. Since show is true, both paragraphs render.
📝 Syntax
intermediate1:30remaining
Which option correctly uses
svelte:fragment to group elements?Select the option that uses
svelte:fragment with correct syntax to group two paragraphs.Attempts:
2 left
💡 Hint
Check the opening and closing tags carefully.
✗ Incorrect
Option A correctly opens and closes the svelte:fragment tag. Other options have missing or incorrect closing tags.
❓ state_output
advanced2:30remaining
What is the output when toggling state inside
svelte:fragment?Given this Svelte component, what text is shown after clicking the button twice?
Svelte
<script> let count = 0; function toggle() { count = (count + 1) % 3; } </script> <svelte:fragment> {#if count === 0} <p>Zero</p> {:else if count === 1} <p>One</p> {:else} <p>Two</p> {/if} <button on:click={toggle}>Toggle</button> </svelte:fragment>
Attempts:
2 left
💡 Hint
Count cycles through 0, 1, 2 with each click.
✗ Incorrect
Initial count is 0 showing 'Zero'. First click sets count to 1 showing 'One'. Second click sets count to 2 showing 'Two'.
🔧 Debug
advanced2:00remaining
Why does this
svelte:fragment code cause a runtime error?Identify the cause of the runtime error in this Svelte snippet:
Svelte
<script> let items = null; </script> <svelte:fragment> {#each items as item} <p>{item}</p> {/each} </svelte:fragment>
Attempts:
2 left
💡 Hint
Check the value of
items before iteration.✗ Incorrect
The {#each} block expects an iterable. Since items is null, it causes a runtime error. svelte:fragment can contain {#each} blocks, so B is incorrect.
🧠 Conceptual
expert1:30remaining
What is the main benefit of using
svelte:fragment in a component?Choose the best explanation for why
svelte:fragment is useful in Svelte components.Attempts:
2 left
💡 Hint
Think about how HTML structure is affected by
svelte:fragment.✗ Incorrect
svelte:fragment groups elements logically in Svelte without creating extra HTML nodes, keeping the DOM clean. It does not add wrappers or replace conditional blocks.