0
0
Svelteframework~20 mins

Component bindings in Svelte - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Component Binding Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this Svelte component with two-way binding?

Consider this Svelte code snippet:

  <script>
    let name = 'Alice';
  </script>

  <input bind:value={name} />
  <p>Hello {name}!</p>

What happens when the user types "Bob" in the input?

Svelte
<script>
  let name = 'Alice';
</script>

<input bind:value={name} />
<p>Hello {name}!</p>
AThe paragraph updates live to show 'Hello Bob!' as the user types.
BThe paragraph stays 'Hello Alice!' until the input loses focus.
CThe input does not update the variable 'name'.
DTyping in the input causes a runtime error.
Attempts:
2 left
💡 Hint

Think about what bind:value does in Svelte.

state_output
intermediate
1:30remaining
What is the value of 'count' after clicking the button twice?

Given this Svelte component:

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

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

What will be displayed after clicking the button two times?

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

<button on:click={() => count += 1}>Increment</button>
<p>Count: {count}</p>
ACount: 0
BCount: 1
CCount: 2
DThe component throws an error on click.
Attempts:
2 left
💡 Hint

Each click increases count by 1.

📝 Syntax
advanced
2:00remaining
Which option correctly binds a child component's prop to a parent variable?

Given a child component <Child /> with a prop value, which parent code correctly binds the parent's parentValue variable to the child's value prop for two-way binding?

A&lt;Child bind:value={parentValue} /&gt;
B&lt;Child value={parentValue} bind:value /&gt;
C&lt;Child bind:value&gt;{parentValue}&lt;/Child&gt;
D&lt;Child bind:value:parentValue /&gt;
Attempts:
2 left
💡 Hint

Remember the Svelte syntax for binding props.

🔧 Debug
advanced
2:30remaining
Why does this Svelte component fail to update the parent variable?

Parent component code:

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

<Child bind:value={text} />

<p>Parent text: {text}</p>

Child component code:

<script>
  export let value;
</script>

<input type="text" value={value} />

Why does typing in the child's input not update the parent's text?

AThe child component must emit a custom event to update the parent.
BThe parent variable <code>text</code> is not declared with <code>let</code>.
CThe parent must use <code>bind:this</code> to link the child.
DThe child input lacks <code>bind:value</code>, so changes don't propagate.
Attempts:
2 left
💡 Hint

Check how the child's input is connected to the prop.

🧠 Conceptual
expert
3:00remaining
What happens if you bind a prop to a constant value in Svelte?

Consider this parent component code:

<Child bind:value={42} />

And the child component has:

<script>
  export let value;
</script>

<input bind:value={value} />

What will happen when the user types in the input?

AThe input is read-only and cannot be changed by the user.
BA runtime error occurs because you cannot bind to a constant.
CThe input updates visually but the parent value remains 42 unchanged.
DThe parent variable 42 updates to the new input value.
Attempts:
2 left
💡 Hint

Think about what binding means and if constants can be updated.