0
0
Svelteframework~10 mins

Auto-subscription with $ prefix 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 auto-subscribe to the store and display its value.

Svelte
<script>
  import { writable } from 'svelte/store';
  const count = writable(0);
</script>
<p>Count: {{$ [1] }}</p>
Drag options to blanks, or click blank then click option'
Aget
Bsubscribe
Ccount
Dset
Attempts:
3 left
💡 Hint
Common Mistakes
Using the store variable without $ prefix, which won't auto-subscribe.
Trying to call subscribe() manually inside the template.
2fill in blank
medium

Complete the code to update the store value on button click using auto-subscription.

Svelte
<script>
  import { writable } from 'svelte/store';
  const count = writable(0);
  function increment() {
    count.[1](n => n + 1);
  }
</script>
<button on:click={increment}>Increment</button>
<p>Count: {$count}</p>
Drag options to blanks, or click blank then click option'
Aset
Bupdate
Csubscribe
Dget
Attempts:
3 left
💡 Hint
Common Mistakes
Using set without the new value.
Trying to use subscribe to change the value.
3fill in blank
hard

Fix the error in the code to correctly auto-subscribe to the store value.

Svelte
<script>
  import { writable } from 'svelte/store';
  const name = writable('Alice');
</script>
<p>Hello, {{$ [1] }}</p>
Drag options to blanks, or click blank then click option'
Aname
B$name
Cname()
Dsubscribe
Attempts:
3 left
💡 Hint
Common Mistakes
Adding an extra $ prefix causing syntax errors.
Trying to call the store as a function.
4fill in blank
hard

Fill both blanks to create a derived store and auto-subscribe to its value.

Svelte
<script>
  import { writable, derived } from 'svelte/store';
  const a = writable(2);
  const b = writable(3);
  const sum = derived([a, b], ([$a, $b]) => $a [1] $b);
</script>
<p>Sum: {{$ [2] }}</p>
Drag options to blanks, or click blank then click option'
A+
Bsum
C-
Da
Attempts:
3 left
💡 Hint
Common Mistakes
Using subtraction instead of addition.
Trying to use one of the original stores instead of the derived store.
5fill in blank
hard

Fill all three blanks to create a reactive statement that logs the store value whenever it changes.

Svelte
<script>
  import { writable } from 'svelte/store';
  const score = writable(0);

  $: console.log([1]); // logs the current score

  function increase() {
    score.[2](n => n + 1);
  }
</script>
<button on:click={increase}>Increase</button>
<p>Score: {{$ [3] }}</p>
Drag options to blanks, or click blank then click option'
A$score
Bupdate
Cscore
Dset
Attempts:
3 left
💡 Hint
Common Mistakes
Using set instead of update for incrementing.
Not using $ prefix in reactive statements causing no reactivity.