0
0
Svelteframework~20 mins

svelte:body for body events - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Svelte Body Event Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
1:30remaining
What happens when you use svelte:body to listen for a click event?
Consider this Svelte component code:
<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>
ABody clicked 3 times.
BBody clicked 1 times.
CBody clicked 0 times.
DBody clicked NaN times.
Attempts:
2 left
💡 Hint
Remember that svelte:body listens to events on the whole document body, not just inside the component.
📝 Syntax
intermediate
1: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?
A<svelte:body on-keydown={handleKeydown} />
B<svelte:body onKeydown={handleKeydown} />
C<svelte:body bind:keydown={handleKeydown} />
D<svelte:body on:keydown={handleKeydown} />
Attempts:
2 left
💡 Hint
Svelte uses the syntax on:eventname for event listeners.
🔧 Debug
advanced
2:00remaining
Why does this svelte:body event listener not update the UI?
Given this code:
<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>
AThe <code>svelte:body</code> tag cannot listen to keydown events.
BThe event handler changes the variable but does not trigger Svelte's reactivity because the variable is not reassigned properly.
CThe variable <code>pressed</code> is not declared with <code>let</code>.
DThe event handler function is missing the event parameter.
Attempts:
2 left
💡 Hint
Svelte updates the UI only when reactive variables are reassigned.
🧠 Conceptual
advanced
1: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.
A<code>svelte:body</code> can only listen to click events, but <code>window</code> can listen to any event.
B<code>svelte:body</code> listens only to events inside the component, while <code>window</code> listeners catch all events globally.
C<code>svelte:body</code> automatically cleans up event listeners when the component unmounts, while manually added <code>window</code> listeners require manual cleanup.
D<code>svelte:body</code> attaches listeners to the document head, while <code>window</code> attaches to the browser window.
Attempts:
2 left
💡 Hint
Think about lifecycle and cleanup of event listeners.
state_output
expert
2:30remaining
What is the value of count after this sequence of events?
Given this Svelte component:
<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>
A1
B2
C3
D-1
Attempts:
2 left
💡 Hint
Remember that right-click triggers the contextmenu event and the preventDefault modifier stops the menu but still runs the handler.