0
0
Svelteframework~20 mins

Inline event handlers in Svelte - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Inline Event Handler Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when clicking the button?

Consider this Svelte component with an inline click event handler:

<script>
  let count = 0;
</script>

<button on:click={() => count += 1}>Click me</button>
<p>Count: {count}</p>

What will the displayed count be after clicking the button 3 times?

Svelte
<script>
  let count = 0;
</script>

<button on:click={() => count += 1}>Click me</button>
<p>Count: {count}</p>
ACount: 3
BCount: 0
CCount: 1
DCount: NaN
Attempts:
2 left
💡 Hint

Think about how the inline arrow function updates the count variable each time the button is clicked.

📝 Syntax
intermediate
2:00remaining
Which inline event handler syntax is correct?

Which of the following inline event handlers in Svelte is syntactically correct for a button click?

A<button on:click={handleClick}>Click</button>
B<button on:click={handleClick()}>Click</button>
C<button on:click=handleClick()>Click</button>
D<button on:click=(handleClick)>Click</button>
Attempts:
2 left
💡 Hint

Remember that in Svelte, event handlers expect a function reference, not the result of calling the function.

🔧 Debug
advanced
2:00remaining
Why does this inline event handler cause an error?

Given this Svelte code snippet:

<script>
  let message = 'Hello';
</script>

<button on:click={() => message.toUpperCase() }>Click</button>
<p>{message}</p>

Why does the displayed message not change after clicking the button?

Svelte
<script>
  let message = 'Hello';
</script>

<button on:click={() => message.toUpperCase() }>Click</button>
<p>{message}</p>
AThe inline handler syntax is invalid and causes a syntax error.
BThe message variable is not updated because toUpperCase() returns a new string but does not change message.
CThe code throws a runtime error because toUpperCase() is not a function.
DThe message variable is updated but the UI does not re-render.
Attempts:
2 left
💡 Hint

Consider whether the message variable is reassigned inside the event handler.

state_output
advanced
2:00remaining
What is the value of count after these clicks?

Analyze this Svelte component:

<script>
  let count = 0;
</script>

<button on:click={() => count++}>Increment</button>
<button on:click={() => count = 0}>Reset</button>
<p>Count: {count}</p>

If the user clicks Increment twice, then Reset once, then Increment once, what is the displayed count?

Svelte
<script>
  let count = 0;
</script>

<button on:click={() => count++}>Increment</button>
<button on:click={() => count = 0}>Reset</button>
<p>Count: {count}</p>
ACount: 0
BCount: 2
CCount: 1
DCount: 3
Attempts:
2 left
💡 Hint

Track each click and how it changes the count variable.

🧠 Conceptual
expert
2:00remaining
Which inline event handler correctly prevents default behavior?

In Svelte, you want to prevent the default form submission when clicking a submit button using an inline event handler. Which option correctly does this?

Svelte
<form on:submit|preventDefault={handleSubmit}>
  <button type="submit">Submit</button>
</form>
A<button on:click={event => event.preventDefault()}>Submit</button>
B<button on:click={handleSubmit}>Submit</button>
C<button on:click={handleSubmit} preventDefault>Submit</button>
D<button on:click|preventDefault={handleSubmit}>Submit</button>
Attempts:
2 left
💡 Hint

Recall how Svelte modifiers like |preventDefault are used inline.