0
0
Svelteframework~20 mins

Group bindings in Svelte - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Svelte Group 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 group binding example?

Consider this Svelte component using group binding with checkboxes:

<script>
  let selected = [];
</script>

<input type="checkbox" bind:group={selected} value="apple" /> Apple
<input type="checkbox" bind:group={selected} value="banana" /> Banana
<input type="checkbox" bind:group={selected} value="cherry" /> Cherry

<p>Selected: {selected.join(', ')}</p>

If the user checks only the "banana" and "cherry" boxes, what will be displayed inside the <p> tag?

ASelected: apple
BSelected: apple, banana, cherry
CSelected: banana, cherry
DSelected:
Attempts:
2 left
💡 Hint

Group binding collects all checked values into the array.

state_output
intermediate
2:00remaining
What is the value of the bound variable after toggling checkboxes?

Given this Svelte snippet:

<script>
  let fruits = ['apple'];
</script>

<input type="checkbox" bind:group={fruits} value="apple" /> Apple
<input type="checkbox" bind:group={fruits} value="banana" /> Banana
<input type="checkbox" bind:group={fruits} value="cherry" /> Cherry

Initially, only "apple" is checked. The user then checks "banana" and unchecks "apple". What is the value of fruits after these changes?

A['banana']
B['banana', 'cherry']
C['apple', 'banana']
D['apple']
Attempts:
2 left
💡 Hint

Group binding updates the array to include only checked values.

📝 Syntax
advanced
2:00remaining
Which option correctly uses group binding with radio buttons in Svelte?

Choose the correct Svelte code snippet that binds a group of radio buttons to a variable color:

A
&lt;input type="radio" bind:group={color} value="red" /&gt; Red
&lt;input type="radio" bind:group={color} value="green" /&gt; Green
&lt;input type="radio" bind:group={color} value="blue" /&gt; Blue
B
&lt;input type="radio" bind:value={color} group="colors" value="red" /&gt; Red
&lt;input type="radio" bind:value={color} group="colors" value="green" /&gt; Green
&lt;input type="radio" bind:value={color} group="colors" value="blue" /&gt; Blue
C
&lt;input type="radio" bind:group={colors} value="red" /&gt; Red
&lt;input type="radio" bind:group={colors} value="green" /&gt; Green
&lt;input type="radio" bind:group={colors} value="blue" /&gt; Blue
D
&lt;input type="checkbox" bind:group={color} value="red" /&gt; Red
&lt;input type="checkbox" bind:group={color} value="green" /&gt; Green
&lt;input type="checkbox" bind:group={color} value="blue" /&gt; Blue
Attempts:
2 left
💡 Hint

Radio buttons use bind:group to bind a single variable to the selected value.

🔧 Debug
advanced
2:00remaining
Why does this Svelte group binding code cause an error?

Examine this Svelte code snippet:

<script>
  let selected = '';
</script>

<input type="checkbox" bind:group={selected} value="one" /> One
<input type="checkbox" bind:group={selected} value="two" /> Two

When running, it throws an error. What is the cause?

AThe bind:group directive is missing a colon.
BCheckbox inputs cannot use bind:group, only radio inputs can.
CThe value attribute must be a number, not a string.
DThe variable 'selected' must be an array, not a string, for checkbox group binding.
Attempts:
2 left
💡 Hint

Check the type of the variable used with bind:group for checkboxes.

🧠 Conceptual
expert
3:00remaining
How does Svelte's group binding handle dynamic changes to the bound array?

In Svelte, if you bind a group of checkboxes to an array variable using bind:group, and then programmatically modify the array (e.g., add or remove values), what happens to the checkboxes?

AThe checkboxes do not update; only user interaction changes their state.
BThe checkboxes update automatically to reflect the current array contents, checking or unchecking accordingly.
CModifying the array causes a runtime error because the binding is one-way.
DOnly the first checkbox updates; others remain unchanged.
Attempts:
2 left
💡 Hint

Think about two-way binding and reactivity in Svelte.