0
0
Svelteframework~20 mins

If blocks ({#if}) in Svelte - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Svelte If Blocks Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What will this Svelte component render?
Consider this Svelte component code:
<script>
  let count = 3;
</script>

{#if count > 5}
  <p>Count is greater than 5</p>
{:else if count > 0}
  <p>Count is positive but 5 or less</p>
{:else}
  <p>Count is zero or negative</p>
{/if}

What will be rendered in the browser?
Svelte
<script>
  let count = 3;
</script>

{#if count > 5}
  <p>Count is greater than 5</p>
{:else if count > 0}
  <p>Count is positive but 5 or less</p>
{:else}
  <p>Count is zero or negative</p>
{/if}
ANo output rendered
B<p>Count is zero or negative</p>
C<p>Count is greater than 5</p>
D<p>Count is positive but 5 or less</p>
Attempts:
2 left
💡 Hint
Check the value of count and which condition matches first.
📝 Syntax
intermediate
2:00remaining
Identify the syntax error in this Svelte if block
Which option correctly fixes the syntax error in this Svelte code?
{#if isActive}
  <p>Active</p>
{:else}
  <p>Inactive</p>
{/if
Svelte
{#if isActive}
  <p>Active</p>
{:else}
  <p>Inactive</p>
{/if
ARemove the closing {/if block entirely
BChange {/if to {/if} to close the block properly
CAdd a semicolon after {/if to fix syntax
DReplace {:else} with {:else if} to fix the error
Attempts:
2 left
💡 Hint
Check the closing tag of the if block carefully.
state_output
advanced
2:00remaining
What is the output after clicking the button twice?
Given this Svelte component:
<script>
  let count = 0;
</script>

{#if count % 2 === 0}
  <p>Even count: {count}</p>
{:else}
  <p>Odd count: {count}</p>
{/if}

<button on:click={() => count++}>Increment</button>

What will the paragraph show after clicking the button two times?
Svelte
<script>
  let count = 0;
</script>

{#if count % 2 === 0}
  <p>Even count: {count}</p>
{:else}
  <p>Odd count: {count}</p>
{/if}

<button on:click={() => count++}>Increment</button>
A<p>Even count: 2</p>
B<p>Odd count: 2</p>
C<p>Even count: 1</p>
D<p>Odd count: 1</p>
Attempts:
2 left
💡 Hint
Count how many times the button increments and check parity.
🔧 Debug
advanced
2:00remaining
Why does this Svelte if block not update on variable change?
This component does not update the displayed text when the button is clicked:
<script>
  let visible = false;
  function toggle() {
    visible = !visible;
  }
</script>

{#if visible}
  <p>Visible</p>
{/if}

<button on:click={toggle}>Toggle</button>

What is the most likely reason the paragraph never appears?
Svelte
<script>
  let visible = false;
  function toggle() {
    visible = !visible;
  }
</script>

{#if visible}
  <p>Visible</p>
{/if}

<button on:click={toggle}>Toggle</button>
AThe code is correct; the paragraph should appear when button is clicked
BThe variable 'visible' is not reactive because it is not declared with $: prefix
CThe component does not re-render because 'visible' is not reassigned properly
DThe toggle function does not update 'visible' because it lacks a return statement
Attempts:
2 left
💡 Hint
Svelte tracks changes to let variables automatically.
🧠 Conceptual
expert
3:00remaining
How does Svelte optimize {#if} blocks during compilation?
Which statement best describes how Svelte handles {#if} blocks when compiling components?
ASvelte compiles all possible {#if} block contents into the DOM and toggles their visibility with CSS
BSvelte uses a virtual DOM diffing algorithm to update the UI when {#if} conditions change
CSvelte compiles {#if} blocks into JavaScript that adds or removes DOM nodes dynamically without a virtual DOM
DSvelte requires manual DOM manipulation code inside {#if} blocks to update the UI
Attempts:
2 left
💡 Hint
Think about how Svelte differs from React in updating the UI.