Challenge - 5 Problems
Svelte Else Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What will this Svelte component display?
Consider this Svelte code snippet:
What text will appear when this component renders?
<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}
Attempts:
2 left
💡 Hint
Check the conditions in order and see which one matches the score value.
✗ Incorrect
The score is 75, which is not >= 90, so the first block is skipped. The else-if checks if score >= 60, which is true, so 'Good' is displayed.
📝 Syntax
intermediate2: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}Attempts:
2 left
💡 Hint
The else-if block requires a condition; else block does not.
✗ Incorrect
The else-if block must have a condition. Here, no condition is given, so it should be replaced with {:else} to handle the fallback case.
🔧 Debug
advanced2:00remaining
Why does this Svelte else-if block never run?
Given this code:
If value is 12, why does the 'Greater than 5' block never show?
{#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}Attempts:
2 left
💡 Hint
Remember how if-else chains work: once a condition matches, the rest are ignored.
✗ Incorrect
When value is 12, the first if condition (value > 10) is true, so Svelte renders that block and skips the else-if and else blocks.
❓ state_output
advanced2:00remaining
What text appears after clicking the button twice?
This Svelte component toggles a number and shows different text:
What text is shown after clicking the button two times?
<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>
Attempts:
2 left
💡 Hint
Count how many times the button is clicked and check the conditions.
✗ Incorrect
After two clicks, count is 2, which matches the else block condition, so 'Many' is displayed.
🧠 Conceptual
expert3: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?
Attempts:
2 left
💡 Hint
Think about how if-else chains work in JavaScript and how Svelte compiles templates.
✗ Incorrect
Svelte compiles the template into JavaScript code that uses nested if-else statements, evaluating conditions in order and rendering only the first matching block.