0
0
Svelteframework~10 mins

Reactive statements ($:) in Svelte - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a reactive statement that updates double when count changes.

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

  $: double = count [1] 2;
</script>
Drag options to blanks, or click blank then click option'
A+
B*
C-
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using + instead of * will add 2 instead of doubling.
Forgetting the $: reactive statement syntax.
2fill in blank
medium

Complete the reactive statement to update message when name changes.

Svelte
<script>
  let name = "";
  let message;

  $: message = `Hello, [1]!`;
</script>
Drag options to blanks, or click blank then click option'
Aname
Bmessage
Ccount
Duser
Attempts:
3 left
💡 Hint
Common Mistakes
Using message inside its own assignment causes errors.
Using unrelated variables like count or user.
3fill in blank
hard

Fix the error in the reactive statement to correctly update total when price or quantity changes.

Svelte
<script>
  let price = 5;
  let quantity = 3;
  let total;

  $: total = price [1] quantity;
</script>
Drag options to blanks, or click blank then click option'
A+
B/
C-
D*
Attempts:
3 left
💡 Hint
Common Mistakes
Using + adds price and quantity instead of multiplying.
Using - or / gives incorrect results.
4fill in blank
hard

Fill both blanks to create a reactive statement that updates fullName by combining firstName and lastName with a space.

Svelte
<script>
  let firstName = "John";
  let lastName = "Doe";
  let fullName;

  $: fullName = [1] + [2];
</script>
Drag options to blanks, or click blank then click option'
AfirstName
B" "
ClastName
DfullName
Attempts:
3 left
💡 Hint
Common Mistakes
Using fullName inside its own assignment causes errors.
Forgetting to add a space between names.
5fill in blank
hard

Fill all three blanks to create a reactive statement that updates fullName by combining firstName, a space, and lastName.

Svelte
<script>
  let firstName = "Jane";
  let lastName = "Smith";
  let fullName;

  $: fullName = [1] + [2] + [3];
</script>
Drag options to blanks, or click blank then click option'
AfirstName
B" "
ClastName
DfullName
Attempts:
3 left
💡 Hint
Common Mistakes
Using fullName inside its own assignment.
Forgetting the space between names.
Mixing up the order of names.