0
0
Svelteframework~20 mins

Rest parameters in Svelte - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Rest Parameters Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What will this Svelte component render?

Consider this Svelte component using rest parameters in props:

<script>
  export let a;
  export let b;
  export let ...rest;
</script>

<div>
  <p>A: {a}</p>
  <p>B: {b}</p>
  <p>Rest keys: {Object.keys(rest).join(", ")}</p>
</div>

If you use this component as <MyComponent a={1} b={2} c={3} d={4} />, what will be rendered inside the <p>Rest keys:</p>?

Svelte
<script>
  export let a;
  export let b;
  export let ...rest;
</script>

<div>
  <p>A: {a}</p>
  <p>B: {b}</p>
  <p>Rest keys: {Object.keys(rest).join(", ")}</p>
</div>
Aa, b
BError: Unexpected token '...'
Cundefined
Dc, d
Attempts:
2 left
💡 Hint

Check if Svelte supports rest parameters in export let declarations.

📝 Syntax
intermediate
2:00remaining
Which is the correct way to collect extra props in a Svelte component?

You want to accept some named props and also collect any other props passed to your Svelte component. Which code snippet correctly achieves this?

A
&lt;script&gt;
  export let a;
  export let b;
  let rest = $$restProps;
&lt;/script&gt;
B
&lt;script&gt;
  export let a;
  export let b;
  const rest = arguments.slice(2);
&lt;/script&gt;
C
&lt;script&gt;
  export let a, b, ...rest;
&lt;/script&gt;
D
&lt;script&gt;
  export let a;
  export let b;
  export let ...rest;
&lt;/script&gt;
Attempts:
2 left
💡 Hint

Look for the special Svelte variable that holds extra props.

state_output
advanced
2:00remaining
What is the output of this Svelte component with rest props?

Given this Svelte component:

<script>
  export let x;
  let rest = $$restProps;
</script>

<div>
  <p>X: {x}</p>
  <p>Rest count: {Object.keys(rest).length}</p>
</div>

If used as <MyComponent x={10} y={20} z={30} />, what will be rendered inside <p>Rest count:</p>?

Svelte
<script>
  export let x;
  let rest = $$restProps;
</script>

<div>
  <p>X: {x}</p>
  <p>Rest count: {Object.keys(rest).length}</p>
</div>
A2
B3
C0
D1
Attempts:
2 left
💡 Hint

Count how many props are passed but not declared with export let.

🔧 Debug
advanced
2:00remaining
Why does this Svelte component fail to compile?

Look at this Svelte component code:

<script>
  export let a;
  export let b;
  export let ...others;
</script>

Why does it fail to compile?

Svelte
<script>
  export let a;
  export let b;
  export let ...others;
</script>
AYou forgot to import 'others' from another file.
BYou must initialize 'others' with an empty object.
CSvelte does not allow rest parameters in export let declarations.
DThe variable 'others' is reserved and cannot be used.
Attempts:
2 left
💡 Hint

Check Svelte's syntax rules for props.

🧠 Conceptual
expert
3:00remaining
How does Svelte handle extra props passed to a component?

In Svelte, when you pass props to a component that are not declared with export let, how can you access those extra props inside the component?

AThey are automatically merged into a variable named 'rest' inside the component.
BExtra props are ignored and cannot be accessed inside the component.
CYou must declare a rest parameter in <code>export let</code> to receive extra props.
DYou can access them via the special variable <code>$$restProps</code> inside the component.
Attempts:
2 left
💡 Hint

Look for Svelte's special variable for extra props.