Challenge - 5 Problems
Svelte Reactivity Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What happens when a reactive variable changes?
Consider this Svelte component:
What will the paragraph show after clicking the button twice?
<script>
let count = 0;
$: doubled = count * 2;
</script>
<button on:click={() => count++}>Increment</button>
<p>Doubled: {doubled}</p>What will the paragraph show after clicking the button twice?
Svelte
<script>
let count = 0;
$: doubled = count * 2;
</script>
<button on:click={() => count++}>Increment</button>
<p>Doubled: {doubled}</p>Attempts:
2 left
💡 Hint
Remember that the reactive statement updates whenever 'count' changes.
✗ Incorrect
The reactive variable 'doubled' recalculates every time 'count' changes. After two clicks, count is 2, so doubled is 4.
❓ state_output
intermediate2:00remaining
What is the output of this reactive statement?
Given this Svelte code:
If we update 'a' to 5 and 'b' to 6 sequentially, what will the paragraph show?
<script>
let a = 3;
let b = 4;
$: sum = a + b;
</script>
<p>Sum: {sum}</p>If we update 'a' to 5 and 'b' to 6 sequentially, what will the paragraph show?
Svelte
<script>
let a = 3;
let b = 4;
$: sum = a + b;
// Simulate updates
a = 5;
b = 6;
</script>
<p>Sum: {sum}</p>Attempts:
2 left
💡 Hint
The reactive statement recalculates after each assignment.
✗ Incorrect
After updating 'a' to 5 and 'b' to 6, sum becomes 5 + 6 = 11, which updates the UI.
📝 Syntax
advanced2:00remaining
Which reactive statement syntax is correct?
Which of the following reactive statements in Svelte will correctly update 'total' when 'x' or 'y' changes?
Attempts:
2 left
💡 Hint
Look for the correct reactive label syntax in Svelte.
✗ Incorrect
In Svelte, reactive statements start with '$:' followed by the assignment. Only option D uses correct syntax.
🔧 Debug
advanced2:00remaining
Why does this reactive statement not update the UI?
Consider this Svelte code:
Why does the paragraph not always show double the count after clicking the button?
<script>
let count = 0;
$: doubleCount = count * 2;
function increment() {
count += 1;
doubleCount = count * 3;
}
</script>
<button on:click={increment}>Increment</button>
<p>Double Count: {doubleCount}</p>Why does the paragraph not always show double the count after clicking the button?
Svelte
<script>
let count = 0;
$: doubleCount = count * 2;
function increment() {
count += 1;
doubleCount = count * 3;
}
</script>
<button on:click={increment}>Increment</button>
<p>Double Count: {doubleCount}</p>Attempts:
2 left
💡 Hint
Think about how reactive statements and manual assignments interact.
✗ Incorrect
The reactive statement '$: doubleCount = count * 2;' runs automatically when 'count' changes. But manually setting 'doubleCount' inside 'increment' breaks this automatic update.
🧠 Conceptual
expert2:00remaining
Why does Svelte use reactivity to update the UI?
Which best explains why Svelte relies on reactivity to update the user interface efficiently?
Attempts:
2 left
💡 Hint
Think about how reactivity helps avoid unnecessary work.
✗ Incorrect
Svelte tracks which variables affect which parts of the UI and updates only those parts when data changes, making updates fast and efficient.