0
0
Svelteframework~20 mins

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

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Svelte Window Events Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when the window is resized?
Consider this Svelte component that uses svelte:window to track window width. What will be shown on the screen after resizing the browser window?
Svelte
<script>
  let width = 0;
  function updateWidth(event) {
    width = event.currentTarget.innerWidth;
  }
</script>

<svelte:window on:resize={updateWidth} />

<p>Window width: {width}</p>
AThe paragraph shows 0 always because width is never changed.
BThe paragraph never updates because the event handler is not called.
CThe paragraph updates live showing the current window width in pixels.
DThe code throws a runtime error because <code>event.currentTarget</code> is undefined.
Attempts:
2 left
💡 Hint
Remember that svelte:window listens to window events and updates reactive variables.
📝 Syntax
intermediate
1:30remaining
Which option correctly listens to the window scroll event?
You want to run a function handleScroll whenever the user scrolls the window. Which Svelte syntax is correct?
Svelte
function handleScroll(event) {
  console.log('scroll position:', window.scrollY);
}
A<window on:scroll={handleScroll} />
B<svelte:window on:scroll={handleScroll} />
C<svelte:document on:scroll={handleScroll} />
D<window:scroll on={handleScroll} />
Attempts:
2 left
💡 Hint
Use the special Svelte element for window events.
🔧 Debug
advanced
2:30remaining
Why does this svelte:window event handler not update the variable?
Look at this code snippet. The scrollY variable should update on scroll but it does not. Why?
Svelte
<script>
  let scrollY = 0;
  function onScroll(event) {
    scrollY = event.target.scrollY;
  }
</script>

<svelte:window on:scroll={onScroll} />

<p>Scroll position: {scrollY}</p>
A<code>event.target.scrollY</code> is undefined; should use <code>window.scrollY</code> instead.
B<code>scrollY</code> is a constant and cannot be reassigned.
CThe <code>onScroll</code> function is never called because the event name is wrong.
DThe <code>svelte:window</code> tag must be self-closing with a slash.
Attempts:
2 left
💡 Hint
Check what event.target refers to in window scroll events.
state_output
advanced
2:00remaining
What is the value of keyPressed after pressing the 'a' key?
This Svelte component listens for keydown events on the window. What will keyPressed contain after pressing the 'a' key?
Svelte
<script>
  let keyPressed = '';
  function onKeydown(event) {
    keyPressed = event.key;
  }
</script>

<svelte:window on:keydown={onKeydown} />

<p>Last key pressed: {keyPressed}</p>
A'a'
B'KeyA'
C'keydown'
D'' (empty string)
Attempts:
2 left
💡 Hint
Check the property of the event that holds the actual key character.
🧠 Conceptual
expert
3:00remaining
Why use svelte:window instead of window.addEventListener in Svelte?
Which reason best explains why svelte:window is preferred over manually adding event listeners to window in Svelte components?
AUsing <code>window.addEventListener</code> is not allowed in Svelte components.
BEvent listeners added with <code>window.addEventListener</code> cannot access component variables.
C<code>svelte:window</code> runs event handlers outside the browser event loop for better performance.
D<code>svelte:window</code> automatically cleans up event listeners when the component is destroyed, preventing memory leaks.
Attempts:
2 left
💡 Hint
Think about component lifecycle and cleanup.