Challenge - 5 Problems
Svelte Body Event Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate1:30remaining
What happens when you use
svelte:body to listen for a click event?Consider this Svelte component code:
What will the paragraph display after clicking anywhere on the page body three times?
<script>
let count = 0;
function handleClick() {
count += 1;
}
</script>
<svelte:body on:click={handleClick} />
<p>Body clicked {count} times.</p>What will the paragraph display after clicking anywhere on the page body three times?
Svelte
<script> let count = 0; function handleClick() { count += 1; } </script> <svelte:body on:click={handleClick} /> <p>Body clicked {count} times.</p>
Attempts:
2 left
💡 Hint
Remember that
svelte:body listens to events on the whole document body, not just inside the component.✗ Incorrect
The
svelte:body tag attaches the event listener to the document body. Each click anywhere on the page triggers handleClick, increasing count. After three clicks, the count is 3.📝 Syntax
intermediate1:00remaining
Which option correctly attaches a keydown event listener to the body using
svelte:body?You want to listen for the 'keydown' event on the whole page body in a Svelte component. Which of the following code snippets is syntactically correct?
Attempts:
2 left
💡 Hint
Svelte uses the syntax
on:eventname for event listeners.✗ Incorrect
The correct syntax for listening to events in Svelte is
on:eventname. So <svelte:body on:keydown={handleKeydown} /> is correct. The others are invalid attribute syntax.🔧 Debug
advanced2:00remaining
Why does this
svelte:body event listener not update the UI?Given this code:
Pressing any key does not update the displayed text. Why?
<script>
let pressed = { pressed: false };
function onKeydown() {
pressed.pressed = true;
}
</script>
<svelte:body on:keydown={onKeydown} />
<p>Key pressed: {pressed.pressed}</p>Pressing any key does not update the displayed text. Why?
Svelte
<script> let pressed = { pressed: false }; function onKeydown() { pressed.pressed = true; } </script> <svelte:body on:keydown={onKeydown} /> <p>Key pressed: {pressed.pressed}</p>
Attempts:
2 left
💡 Hint
Svelte updates the UI only when reactive variables are reassigned.
✗ Incorrect
Svelte's reactivity system detects top-level assignments to variables declared with
let. Mutating nested properties, like pressed.pressed = true, does not trigger reactivity. To update the UI, reassign the object itself, such as pressed = { ...pressed, pressed: true }.🧠 Conceptual
advanced1:30remaining
What is the main difference between
svelte:body and adding event listeners directly to window in Svelte?Choose the best explanation for how
svelte:body differs from adding event listeners directly to window in a Svelte component.Attempts:
2 left
💡 Hint
Think about lifecycle and cleanup of event listeners.
✗ Incorrect
svelte:body is a Svelte feature that attaches event listeners to the document body and automatically removes them when the component is destroyed. When adding listeners directly to window, you must manually remove them to avoid memory leaks.❓ state_output
expert2:30remaining
What is the value of
count after this sequence of events?Given this Svelte component:
What is the value of
1. Left-click anywhere on the page body
2. Right-click anywhere on the page body
3. Left-click anywhere on the page body
<script>
let count = 0;
function increment() {
count += 1;
}
function decrement() {
count -= 1;
}
</script>
<svelte:body on:click={increment} on:contextmenu|preventDefault={decrement} />
<p>Count: {count}</p>What is the value of
count after the user performs these actions in order?1. Left-click anywhere on the page body
2. Right-click anywhere on the page body
3. Left-click anywhere on the page body
Svelte
<script> let count = 0; function increment() { count += 1; } function decrement() { count -= 1; } </script> <svelte:body on:click={increment} on:contextmenu|preventDefault={decrement} /> <p>Count: {count}</p>
Attempts:
2 left
💡 Hint
Remember that right-click triggers the contextmenu event and the preventDefault modifier stops the menu but still runs the handler.
✗ Incorrect
Step 1: Left-click triggers increment → count = 1
Step 2: Right-click triggers contextmenu with preventDefault and decrement → count = 0
Step 3: Left-click triggers increment → count = 1
Final count is 1.