0
0
Svelteframework~20 mins

Compiler-based approach (no virtual DOM) in Svelte - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Svelte Compiler Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the rendered output of this Svelte component?

Consider this Svelte component code:

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

<button on:click={increment}>Clicked {count} times</button>

What will the button display after clicking it 3 times?

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

<button on:click={increment}>Clicked {count} times</button>
AClicked NaN times
BClicked count times
CClicked 0 times
DClicked 3 times
Attempts:
2 left
💡 Hint

Think about how the count variable changes on each click and how Svelte updates the DOM.

📝 Syntax
intermediate
2:00remaining
Which option causes a syntax error in Svelte?

Which of these Svelte snippets will cause a syntax error when compiled?

A<script>let flag = true;</script><p>{#if flag}Yes{/if}</p>
B<script>let count = 0;</script><button on:click={count++}>Click</button>
C<script>let items = [1,2,3];</script><ul>{#each items as item}<li>{item}</li>{/each}</ul>
D<script>let name = 'Svelte';</script><p>Hello {name}!</p>
Attempts:
2 left
💡 Hint

Check how event handlers expect functions, not expressions.

🔧 Debug
advanced
2:30remaining
Why does this Svelte component not update the UI on state change?

Look at this Svelte code:

<script>
  let obj = { value: 0 };
  function increment() {
    obj.value += 1;
  }
</script>

<button on:click={increment}>Value: {obj.value}</button>

Clicking the button does not update the displayed value. Why?

Svelte
<script>
  let obj = { value: 0 };
  function increment() {
    obj.value += 1;
  }
</script>

<button on:click={increment}>Value: {obj.value}</button>
ASvelte does not detect changes inside objects unless the object reference changes
BSvelte requires a store to update nested object properties
CThe variable obj is not declared with let
DThe increment function is not called on click
Attempts:
2 left
💡 Hint

Think about how Svelte tracks changes to variables.

🧠 Conceptual
advanced
2:00remaining
What is a key advantage of Svelte's compiler-based approach over virtual DOM frameworks?

Choose the best explanation for why Svelte does not use a virtual DOM.

ASvelte requires manual DOM updates by the developer to improve performance
BSvelte uses a virtual DOM but hides it from the developer for simplicity
CSvelte compiles components to efficient imperative code that updates the DOM directly, reducing runtime overhead
DSvelte delays DOM updates until all components finish rendering to batch changes
Attempts:
2 left
💡 Hint

Think about how Svelte generates code before the app runs.

state_output
expert
3:00remaining
What is the final rendered output after these state changes in Svelte?

Given this Svelte component:

<script>
  let count = 1;
  $: doubled = count * 2;
  function update() {
    count = 3;
    count = 5;
  }
</script>

<button on:click={update}>Count: {count}, Doubled: {doubled}</button>

What will the button display after clicking it once?

Svelte
<script>
  let count = 1;
  $: doubled = count * 2;
  function update() {
    count = 3;
    count = 5;
  }
</script>

<button on:click={update}>Count: {count}, Doubled: {doubled}</button>
ACount: 5, Doubled: 10
BCount: 3, Doubled: 6
CCount: 1, Doubled: 2
DCount: 5, Doubled: 6
Attempts:
2 left
💡 Hint

Consider how Svelte processes reactive statements and variable assignments.