Challenge - 5 Problems
Svelte Input Binding Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate1:30remaining
What is the output when typing in the input?
Consider this Svelte component:
<script>
let name = '';
</script>
<input bind:value={name} />
<p>Hello {name}!
What will the paragraph show after typing Anna in the input?Svelte
<script> let name = ''; </script> <input bind:value={name} /> <p>Hello {name}!</p>
Attempts:
2 left
💡 Hint
The bind:value keeps the variable updated with the input's content.
✗ Incorrect
The bind:value directive links the input's value to the variable 'name'. When you type 'Anna', 'name' updates immediately, so the paragraph shows 'Hello Anna!'.
❓ state_output
intermediate1:30remaining
What is the value of 'count' after clicking the button twice?
Given this Svelte component:
If the user clicks the button two times without changing the input manually, what will the paragraph show?
<script>
let count = 0;
</script>
<input type="number" bind:value={count} />
<button on:click={() => count += 1}>Add 1</button>
<p>Count is {count}</p>If the user clicks the button two times without changing the input manually, what will the paragraph show?
Svelte
<script> let count = 0; </script> <input type="number" bind:value={count} /> <button on:click={() => count += 1}>Add 1</button> <p>Count is {count}</p>
Attempts:
2 left
💡 Hint
The button increases the count variable by 1 each click.
✗ Incorrect
The count starts at 0. Each button click adds 1. After two clicks, count is 2, so the paragraph shows 'Count is 2'.
📝 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 bind:checked, not bind:value.
✗ Incorrect
For checkboxes, Svelte uses bind:checked to link the boolean variable to the checkbox state. bind:value is for text inputs.
🔧 Debug
advanced2:00remaining
Why does this input not update the variable?
Look at this Svelte code:
Typing in the input does not update the paragraph. What is the problem?
<script>
let text = '';
</script>
<input bind:value=text />
<p>Text: {text}</p>Typing in the input does not update the paragraph. What is the problem?
Svelte
<script> let text = ''; </script> <input bind:value=text /> <p>Text: {text}</p>
Attempts:
2 left
💡 Hint
bind:value expects an expression inside curly braces.
✗ Incorrect
In Svelte, bind:value requires curly braces around the variable name to bind properly. Without them, it treats 'text' as a string literal, so the variable does not update.
🧠 Conceptual
expert2:30remaining
What happens when binding a number input to a string variable?
Consider this Svelte code:
If the user types 30 in the input, what will the paragraph show?
<script>
let age = '';
</script>
<input type="number" bind:value={age} />
<p>Age is {age} ({typeof age})</p>If the user types 30 in the input, what will the paragraph show?
Svelte
<script> let age = ''; </script> <input type="number" bind:value={age} /> <p>Age is {age} ({typeof age})</p>
Attempts:
2 left
💡 Hint
bind:value on number inputs still binds as string by default.
✗ Incorrect
Even though the input type is number, bind:value binds the value as a string. So typing 30 sets age to the string '30', not the number 30.