Challenge - 5 Problems
Svelte Binding Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What happens when you bind an input value in Svelte?
Consider this Svelte code snippet:
What will be the output if the user types 'Bob' in the input box?
<script>
let name = 'Alice';
</script>
<input bind:value={name} />
<p>Hello {name}!</p>What will be the output if the user types 'Bob' in the input box?
Svelte
<script> let name = 'Alice'; </script> <input bind:value={name} /> <p>Hello {name}!</p>
Attempts:
2 left
💡 Hint
Think about how binding connects the input and the variable.
✗ Incorrect
Binding the input's value to the variable 'name' means changes in the input update 'name', and changes in 'name' update the input. This creates two-way data flow.
❓ state_output
intermediate2:00remaining
What is the value of the variable after input change with binding?
Given this Svelte component:
If the user types '5' in the input, what is the value of 'count'?
<script>
let count = 0;
</script>
<input type='number' bind:value={count} />
<p>Count is {count}</p>If the user types '5' in the input, what is the value of 'count'?
Svelte
<script> let count = 0; </script> <input type='number' bind:value={count} /> <p>Count is {count}</p>
Attempts:
2 left
💡 Hint
Remember how HTML inputs handle values and how Svelte binds them.
✗ Incorrect
Input values are strings by default. Binding assigns the string '5' to 'count'. To convert to number, extra code is needed.
📝 Syntax
advanced2:00remaining
Which option correctly binds a checkbox to a boolean variable?
You want to bind a checkbox input to a boolean variable 'checked' in Svelte. Which code snippet is correct?
Attempts:
2 left
💡 Hint
Checkboxes use 'checked' property, not 'value', for binding booleans.
✗ Incorrect
In Svelte, to bind a checkbox's checked state, use bind:checked with a variable. Using bind:value or quotes is incorrect.
🔧 Debug
advanced2:00remaining
Why does this binding not update the variable?
Look at this Svelte code:
Why does typing in the input not change the 'text' variable?
<script>
let text = 'hello';
</script>
<input value={text} />
<p>Text: {text}</p>Why does typing in the input not change the 'text' variable?
Svelte
<script> let text = 'hello'; </script> <input value={text} /> <p>Text: {text}</p>
Attempts:
2 left
💡 Hint
Binding requires 'bind:' prefix to connect input and variable.
✗ Incorrect
Using 'value={text}' sets the input's initial value but does not update 'text' on input changes. Binding with 'bind:value' is needed for two-way data flow.
🧠 Conceptual
expert3:00remaining
Why do bindings enable two-way data flow in Svelte?
Which explanation best describes why Svelte's binding syntax enables two-way data flow between a variable and an input element?
Attempts:
2 left
💡 Hint
Think about how changes in input and variable reflect each other instantly.
✗ Incorrect
Svelte's binding sets up event listeners to update the variable on input changes and also updates the input when the variable changes, creating a seamless two-way connection.