Bird
Raised Fist0
Svelteframework~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. In Svelte, which block is used to check a second condition if the first {#if} condition is false?
easy
A. {:else}
B. {#each}
C. {:else if}
D. {#await}

Solution

  1. Step 1: Understand the purpose of {#if} and {:else if}

    The {#if} block checks the first condition. If it is false, {:else if} allows checking another condition.
  2. Step 2: Identify the correct block for a second condition

    {:else if} is specifically designed to check additional conditions after the first fails.
  3. Final Answer:

    {:else if} -> Option C
  4. Quick Check:

    Second condition check = {:else if} [OK]
Hint: Use {:else if} to test another condition after {#if} fails [OK]
Common Mistakes:
  • Using {:else} to check a condition instead of fallback
  • Confusing {#each} with conditional blocks
  • Trying to use {#await} for conditions
2. Which of the following is the correct syntax for an else-if block in Svelte?
easy
A. {:elseif condition}
B. {:else if condition}
C. {:else-if condition}
D. {:else if (condition)}

Solution

  1. Step 1: Recall Svelte syntax for else-if

    Svelte uses {:else if} followed by the condition without parentheses.
  2. Step 2: Compare options to correct syntax

    {:else if condition} matches the correct syntax exactly: {:else if condition}
  3. Final Answer:

    {:else if condition} -> Option B
  4. Quick Check:

    Correct else-if syntax = {:else if condition} [OK]
Hint: Remember: space between else and if, no parentheses [OK]
Common Mistakes:
  • Writing {:elseif} as one word
  • Adding parentheses around condition
  • Using hyphen in else-if
3. What will be rendered by this Svelte code?
{`\n{#if score >= 90}\n  

Excellent

\n{:else if score >= 60}\n

Good

\n{:else}\n

Try again

\n{/if}`}
medium
A.

Good

B.

Try again

C.

Excellent

D. No output

Solution

  1. Step 1: Evaluate the first condition

    score is 75, which is not >= 90, so the first {#if} block is false.
  2. Step 2: Evaluate the else-if condition

    score is 75, which is >= 60, so {:else if} block is true and renders <p>Good</p>.
  3. Final Answer:

    <p>Good</p> -> Option A
  4. Quick Check:

    score 75 triggers else-if block = Good [OK]
Hint: Check conditions top-down; first true block renders [OK]
Common Mistakes:
  • Assuming all blocks render instead of only one
  • Misreading the score value
  • Confusing else-if with else
4. Identify the error in this Svelte code snippet:
{`{#if isActive}\n  

Active

\n{:else if}\n

Inactive

\n{/if}`}
medium
A. Missing condition after {:else if}
B. Incorrect closing tag {/if}
C. Cannot use {:else if} after {#if}
D. Missing colon before else

Solution

  1. Step 1: Check {:else if} syntax

    The {:else if} block requires a condition to follow it, but none is provided here.
  2. Step 2: Confirm other parts are correct

    The {#if} and {/if} tags are correctly placed, and {:else if} is allowed after {#if}.
  3. Final Answer:

    Missing condition after {:else if} -> Option A
  4. Quick Check:

    {:else if} needs a condition [OK]
Hint: Always add a condition after {:else if} [OK]
Common Mistakes:
  • Leaving {:else if} without condition
  • Confusing {:else} with {:else if}
  • Misplacing {/if} tag
5. Given this Svelte code:
{`\n{#if status === 'loading'}\n  

Loading...

\n{:else if status === 'success'}\n

Data loaded

\n{:else if status === 'error'}\n

Error occurred

\n{:else}\n

Idle

\n{/if}`}

What will be displayed if status is an empty string?
hard
A.

Loading...

B.

Data loaded

C.

Error occurred

D.

Idle

Solution

  1. Step 1: Check each condition against empty string

    status is '', so none of the conditions 'loading', 'success', or 'error' match.
  2. Step 2: Use {:else} block as fallback

    Since no conditions match, the {:else} block runs, rendering <p>Idle</p>.
  3. Final Answer:

    <p>Idle</p> -> Option D
  4. Quick Check:

    Empty string triggers else block = Idle [OK]
Hint: If no conditions match, {:else} block runs [OK]
Common Mistakes:
  • Assuming empty string matches a condition
  • Forgetting {:else} fallback
  • Mixing up else-if and else blocks
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard