0
0
Svelteframework~10 mins

First Svelte component - 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 simple Svelte component that displays a greeting.

Svelte
<script>
  let name = "World";
</script>

<h1>Hello, [1]!</h1>
Drag options to blanks, or click blank then click option'
Ausername
BName
Cname
Dgreeting
Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable name that was not declared.
Forgetting to use curly braces around the variable.
2fill in blank
medium

Complete the code to bind the input value to the name variable.

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

<input type="text" [1]={name} />
<p>Hello, {name}!</p>
Drag options to blanks, or click blank then click option'
Abind:value
Bvalue
Cbind
Dmodel
Attempts:
3 left
💡 Hint
Common Mistakes
Using just value attribute which does not update the variable.
Using incorrect binding syntax like bind or model.
3fill in blank
hard

Fix the error in the component to correctly display the message only if name is not empty.

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

[1] (name) {
  <p>Hello, {name}!</p>
}
Drag options to blanks, or click blank then click option'
Aif
B{#if
C{#each
Dwhile
Attempts:
3 left
💡 Hint
Common Mistakes
Using plain JavaScript if without Svelte block syntax.
Using {#each} which is for loops, not conditions.
4fill in blank
hard

Fill both blanks to create a button that increments a counter and displays it.

Svelte
<script>
  let count = 0;
  function increment() {
    count [1] 1;
  }
</script>

<button on:click=[2]>Add 1</button>
<p>Count: {count}</p>
Drag options to blanks, or click blank then click option'
A+=
B-=
Cincrement
Ddecrement
Attempts:
3 left
💡 Hint
Common Mistakes
Using -= which subtracts instead of adds.
Using wrong function name or missing the event handler.
5fill in blank
hard

Fill all three blanks to create a reactive statement that doubles the count and displays it.

Svelte
<script>
  let count = 0;
  $: [1] = [2] * 2;
</script>

<button on:click=[3]>() => count += 1</button>
<p>Double: {double}</p>
Drag options to blanks, or click blank then click option'
Adouble
Bcount
C() => count += 1
Dincrement
Attempts:
3 left
💡 Hint
Common Mistakes
Not using the reactive $: syntax.
Using wrong variable names or missing the arrow function syntax.