0
0
Svelteframework~20 mins

Using data in pages in Svelte - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Svelte Data Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
1:30remaining
What will this Svelte page display?

Consider this Svelte page code:

  <script>
    export let name = 'Guest';
  </script>

  <h1>Hello {name}!
  </h1>

What will the page show if no name is passed?

Svelte
<script>
  export let name = 'Guest';
</script>

<h1>Hello {name}!</h1>
AHello undefined!
BHello !
CHello Guest!
DError: name is not defined
Attempts:
2 left
💡 Hint

Check the default value assigned to name.

state_output
intermediate
1:30remaining
What is the output after clicking the button twice?

Given this Svelte page:

<script>
  let count = 0;
  function increment() {
    count += 1;
  }
</script>

<button on:click={increment}>Click me</button>
<p>Clicked {count} times</p>

What will the paragraph show after clicking the button twice?

Svelte
<script>
  let count = 0;
  function increment() {
    count += 1;
  }
</script>

<button on:click={increment}>Click me</button>
<p>Clicked {count} times</p>
AClicked 2 times
BClicked 1 times
CClicked 0 times
DError: increment is not a function
Attempts:
2 left
💡 Hint

Each click calls increment which adds 1 to count.

📝 Syntax
advanced
2:00remaining
Which option correctly fetches data in a Svelte page?

You want to fetch JSON data from /api/data when the page loads and store it in items. Which code snippet is correct?

A
&lt;script&gt;
  import { onMount } from 'svelte';
  let items = [];
  onMount(async () =&gt; {
    const res = await fetch('/api/data');
    items = await res.json();
  });
&lt;/script&gt;
B
&lt;script&gt;
  let items = [];
  fetch('/api/data')
    .then(res =&gt; res.json())
    .then(data =&gt; items = data);
&lt;/script&gt;
C
&lt;script&gt;
  let items = [];
  async function load() {
    const res = await fetch('/api/data');
    items = await res.json();
  }
  load();
&lt;/script&gt;
D
&lt;script&gt;
  let items = [];
  fetch('/api/data').json().then(data =&gt; items = data);
&lt;/script&gt;
Attempts:
2 left
💡 Hint

Think about when the fetch runs and how to handle async in Svelte.

🔧 Debug
advanced
2:00remaining
Why does this Svelte page not update the displayed name?

Look at this code:

<script>
  export let user = { name: 'Alice' };
  function changeName() {
    user.name = 'Bob';
  }
</script>

<h1>Hello {user.name}!</h1>
<button on:click={changeName}>Change Name</button>

Clicking the button does not update the displayed name. Why?

Svelte
<script>
  export let user = { name: 'Alice' };
  function changeName() {
    user.name = 'Bob';
  }
</script>

<h1>Hello {user.name}!</h1>
<button on:click={changeName}>Change Name</button>
AThe function changeName is not called because the button has no on:click event.
BThe syntax for updating user.name is incorrect; it should use user.setName('Bob').
CThe user variable is read-only because it is exported and cannot be changed.
DSvelte does not detect changes to object properties; you must assign a new object to trigger update.
Attempts:
2 left
💡 Hint

Think about how Svelte tracks changes to variables and objects.

🧠 Conceptual
expert
2:30remaining
What is the correct way to pass data from a Svelte page to a nested component?

You have a Svelte page that fetches a list of products. You want to pass this list to a nested <ProductList> component. Which approach correctly passes the data?

A<ProductList export let products={products} />
B<ProductList products={products} />
C<ProductList let:products={products} />
D<ProductList bind:products={products} />
Attempts:
2 left
💡 Hint

Recall how props are passed in Svelte components.