0
0
Svelteframework~20 mins

First Svelte component - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Svelte Component Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What does this Svelte component display?
Consider this simple Svelte component code. What will be shown on the webpage when it renders?
Svelte
<script>
  let name = 'Alice';
</script>

<h1>Hello {name}!</h1>
AHello {name}!
BHello Alice!
CHello !
DError: name is not defined
Attempts:
2 left
💡 Hint
Look at how the variable 'name' is used inside curly braces in the HTML.
📝 Syntax
intermediate
2:00remaining
Which option correctly defines a Svelte component with a button that increments a counter?
Choose the code that correctly creates a Svelte component with a button. Clicking the button increases a number shown on the page.
A
&lt;script&gt;
  let count = 0
  function increment() {
    count = count + 1
  }
&lt;/script&gt;
&lt;button on:click=increment&gt;{count}&lt;/button&gt;
B
&lt;script&gt;
  let count = 0;
  function increment() {
    count++
  }
&lt;/script&gt;
&lt;button onclick={increment}&gt;{count}&lt;/button&gt;
C
&lt;script&gt;
  let count = 0;
  function increment() {
    count += 1
  }
&lt;/script&gt;
&lt;button onClick={increment}&gt;{count}&lt;/button&gt;
D
&lt;script&gt;
  let count = 0;
  function increment() {
    count += 1;
  }
&lt;/script&gt;
&lt;button on:click={increment}&gt;{count}&lt;/button&gt;
Attempts:
2 left
💡 Hint
Svelte uses 'on:click' for event handlers and requires braces around the handler function.
state_output
advanced
2:00remaining
What is the displayed count after clicking the button twice?
Given this Svelte component, what number will be shown after clicking the button two times?
Svelte
<script>
  let count = 0;
  function increment() {
    count += 1;
  }
</script>
<button on:click={increment}>{count}</button>
A0
B1
C2
DError: count is not reactive
Attempts:
2 left
💡 Hint
Each click runs the increment function which adds 1 to count.
🔧 Debug
advanced
2:00remaining
Why does this Svelte component not update the count when the button is clicked?
Look at this code. Clicking the button does not change the number shown. What is the reason?
Svelte
<script>
  let count = 0;
  function increment() {
    count = count + 1;
  }
</script>
<button on:click=increment>{count}</button>
AThe event handler is missing braces around 'increment': should be 'on:click={increment}' not 'on:click=increment'.
BThe variable 'count' must be declared with 'const' to update.
CThe function 'increment' must return the new count value.
DThe button element cannot display variables inside curly braces.
Attempts:
2 left
💡 Hint
Check the event attribute syntax in Svelte, especially the braces around the handler.
🧠 Conceptual
expert
2:00remaining
What happens if you declare a variable with 'const' in a Svelte component and try to update it?
In Svelte, if you write const count = 0; and then try to increase count inside a function, what will happen?
AA runtime error occurs because 'count' cannot be reassigned.
BThe count updates normally because Svelte tracks all variables.
CThe count updates but the UI does not reflect the change.
DThe component compiles but clicking the button does nothing silently.
Attempts:
2 left
💡 Hint
Think about how 'const' works in JavaScript and Svelte's reactivity.