0
0
Svelteframework~20 mins

Progressive enhancement in Svelte - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Progressive Enhancement Mastery
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 initially?

Consider this Svelte component that uses progressive enhancement to show a message only if JavaScript is enabled.

<noscript>Please enable JavaScript to see the message.</noscript>
<script>
  let showMessage = false;
  onMount(() => {
    showMessage = true;
  });
</script>

{#if showMessage}
  <p>JavaScript is enabled!</p>
{/if}

What will the user see if JavaScript is disabled?

Svelte
<noscript>Please enable JavaScript to see the message.</noscript>
<script>
  import { onMount } from 'svelte';
  let showMessage = false;
  onMount(() => {
    showMessage = true;
  });
</script>

{#if showMessage}
  <p>JavaScript is enabled!</p>
{/if}
AThe message 'JavaScript is enabled!' is shown.
BNo message is shown at all.
CThe message 'Please enable JavaScript to see the message.' is shown.
DBoth messages are shown simultaneously.
Attempts:
2 left
💡 Hint

Think about what the <noscript> tag does when JavaScript is disabled.

state_output
intermediate
2:00remaining
What is the final value of count after this Svelte component runs?

This Svelte component increments a counter only if JavaScript is enabled. What is the value of count after the component mounts?

<noscript></noscript>
<script>
  import { onMount } from 'svelte';
  let count = 0;
  onMount(() => {
    count += 1;
  });
</script>

<p>Count: {count}</p>
Svelte
<noscript></noscript>
<script>
  import { onMount } from 'svelte';
  let count = 0;
  onMount(() => {
    count += 1;
  });
</script>

<p>Count: {count}</p>
A1
B0
Cundefined
DNaN
Attempts:
2 left
💡 Hint

Remember when onMount runs in Svelte.

📝 Syntax
advanced
2:00remaining
Which option correctly implements progressive enhancement for a button that works without JavaScript?

You want a button that submits a form normally if JavaScript is disabled, but uses Svelte's event handler when JavaScript is enabled. Which code snippet achieves this?

A<button type="submit" on:click={handleClick}>Submit</button>
B<button type="submit" on:click|preventDefault={handleClick}>Submit</button>
C<button type="button" on:click={handleClick}>Submit</button>
D<button on:click={handleClick}>Submit</button>
Attempts:
2 left
💡 Hint

Think about the default behavior of a submit button and how to preserve it when JavaScript is disabled.

🔧 Debug
advanced
2:00remaining
Why does this Svelte progressive enhancement code fail to show the fallback message?

Given this code, the fallback message inside <noscript> does not appear when JavaScript is disabled. Why?

<script>
  let jsEnabled = false;
  onMount(() => {
    jsEnabled = true;
  });
</script>

{#if !jsEnabled}
  <noscript>Please enable JavaScript</noscript>
{/if}
Svelte
<script>
  import { onMount } from 'svelte';
  let jsEnabled = false;
  onMount(() => {
    jsEnabled = true;
  });
</script>

{#if !jsEnabled}
  <noscript>Please enable JavaScript</noscript>
{/if}
AThe <noscript> tag cannot be used inside Svelte blocks.
BThe <noscript> tag is inside a Svelte conditional, so it never renders when JS is disabled.
CThe variable jsEnabled is never updated because onMount does not run without JS.
DThe condition !jsEnabled is always false, so the block never shows.
Attempts:
2 left
💡 Hint

Consider how <noscript> works and when Svelte renders blocks.

🧠 Conceptual
expert
2:00remaining
Which statement best describes progressive enhancement in Svelte applications?

Choose the statement that most accurately explains how progressive enhancement works in Svelte.

ASvelte uses progressive enhancement by disabling JavaScript features if the user prefers reduced motion.
BSvelte apps always require JavaScript; progressive enhancement is not applicable.
CProgressive enhancement in Svelte means loading all components on the server and never using client-side JavaScript.
DProgressive enhancement means providing basic HTML content that works without JavaScript, then enhancing it with Svelte's reactive features when JS is enabled.
Attempts:
2 left
💡 Hint

Think about how Svelte can deliver content that works with or without JavaScript.