0
0
Svelteframework~20 mins

Else and else-if blocks in Svelte - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Svelte Else 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 display?
Consider this Svelte code snippet:
<script>
  let score = 75;
</script>

{#if score >= 90}
  <p>Excellent</p>
{:else if score >= 60}
  <p>Good</p>
{:else}
  <p>Needs Improvement</p>
{/if}

What text will appear when this component renders?
Svelte
<script>
  let score = 75;
</script>

{#if score >= 90}
  <p>Excellent</p>
{:else if score >= 60}
  <p>Good</p>
{:else}
  <p>Needs Improvement</p>
{/if}
AGood
BNeeds Improvement
CExcellent
DNo text will appear
Attempts:
2 left
💡 Hint
Check the conditions in order and see which one matches the score value.
📝 Syntax
intermediate
2:00remaining
Identify the syntax error in this Svelte conditional block
Which option correctly fixes the syntax error in this Svelte code?
{#if loggedIn}
  <p>Welcome back!</p>
{:else if}
  <p>Please log in.</p>
{/if}
Svelte
{#if loggedIn}
  <p>Welcome back!</p>
{:else if}
  <p>Please log in.</p>
{/if}
A{:else if loggedIn === false}
B{:else}
C{:else if !loggedIn}
D{:else if}
Attempts:
2 left
💡 Hint
The else-if block requires a condition; else block does not.
🔧 Debug
advanced
2:00remaining
Why does this Svelte else-if block never run?
Given this code:
{#if value > 10}
  <p>Greater than 10</p>
{:else if value > 5}
  <p>Greater than 5</p>
{:else}
  <p>5 or less</p>
{/if}

If value is 12, why does the 'Greater than 5' block never show?
Svelte
{#if value > 10}
  <p>Greater than 10</p>
{:else if value > 5}
  <p>Greater than 5</p>
{:else}
  <p>5 or less</p>
{/if}
ABecause else-if blocks are not supported in Svelte
BBecause else-if conditions must be checked before if
CBecause value 12 is not greater than 5
DBecause the first if condition is true, so else-if is skipped
Attempts:
2 left
💡 Hint
Remember how if-else chains work: once a condition matches, the rest are ignored.
state_output
advanced
2:00remaining
What text appears after clicking the button twice?
This Svelte component toggles a number and shows different text:
<script>
  let count = 0;
</script>

{#if count === 0}
  <p>Zero</p>
{:else if count === 1}
  <p>One</p>
{:else}
  <p>Many</p>
{/if}

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

What text is shown after clicking the button two times?
Svelte
<script>
  let count = 0;
</script>

{#if count === 0}
  <p>Zero</p>
{:else if count === 1}
  <p>One</p>
{:else}
  <p>Many</p>
{/if}

<button on:click={() => count++}>Add</button>
AZero
BNo text
CMany
DOne
Attempts:
2 left
💡 Hint
Count how many times the button is clicked and check the conditions.
🧠 Conceptual
expert
3:00remaining
How does Svelte handle multiple else-if blocks internally?
In Svelte, when you write multiple {:else if} blocks in a conditional, how does Svelte process them when rendering?
ASvelte compiles them into nested if-else JavaScript statements, checking conditions in order
BSvelte runs all else-if conditions in parallel and renders all matching blocks
CSvelte converts else-if blocks into separate components rendered conditionally
DSvelte ignores else-if blocks and only uses the first if and else blocks
Attempts:
2 left
💡 Hint
Think about how if-else chains work in JavaScript and how Svelte compiles templates.