Student starter code (30% baseline)
index.html- Main HTML pagescript.js- JavaScript logicstyles.css- Styling and layoutpackage.json- Dependenciessetup.sh- Setup scriptREADME.md- Instructions (below)๐ก Download the ZIP, extract it, and follow the instructions below to get started!
W3 Server-Side Development & Authentication - Lesson 5
By completing this activity, you will:
$: syntaxTotal Activity Time: 45 minutes
IMPORTANT: This template includes a WORKING foundation with 65% pre-built!
App.svelte in this folder# No installation needed - just copy to Svelte REPL!
65% of the code is implemented for you:
File: App.svelte - Line 11
Time: 5 minutes
Add a reactive declaration that multiplies number by 6 automatically.
Success Criteria:
$: syntax for reactive declarationmultiplynumber * 6Hint: Reactive declarations start with $: and run whenever their dependencies change.
Example Pattern:
$: variableName = calculation;
File: App.svelte - Line 14
Time: 5 minutes
Add a reactive statement that prevents number from going below 0.
Success Criteria:
$: syntax for reactive statementnumber < 0number to 0Hint: Reactive statements can contain if conditions and will run automatically when dependencies change.
Example Pattern:
$: if (condition) {
alert("message");
variable = resetValue;
}
File: App.svelte - Line 23
Time: 5 minutes
Add a reactive statement that prevents number from going above 12.
Success Criteria:
$: syntax for reactive statementnumber > 12number to 0Hint: This follows the same pattern as TODO #2, just with different condition and message.
File: App.svelte - Lines 32-62
Time: 15 minutes
Extend the calculator to multiply two different numbers.
Success Criteria:
number2 variable initialized to 0multiply to use number * number2decrease2() and increase2() functions|| operator{number} x {number2} = {multiply}Hint: Follow the pattern of the existing number1 functions and buttons. Use HTML entities < and > for < and > symbols.
By examining the working code and completing the TODOs, you'll understand:
$: variable = expression creates auto-updating computed values$: if (condition) { ... } runs code when dependencies changeTest these scenarios:
Exercise 1 (Already Working):
Exercise 2 (After TODO #1-3):
Exercise 3 (After TODO #4):
Your project is complete when:
Exercise 1 (Already Complete):
Exercise 2:
multiply variable reactively calculates number * 6{number} x 6 = {multiply}Exercise 3:
number * number2{number} x {number2} = {multiply}$:)// Runs whenever 'number' changes
$: multiply = number * 6;
// You can also use complex expressions
$: total = number1 + number2 + number3;
// Runs whenever 'number' changes
$: if (number < 0) {
alert("Too low!");
number = 0;
}
// Runs when either number1 OR number2 changes
$: if (number1 > 10 || number2 > 10) {
alert("One number is too high!");
}
Issue: Multiplication not updating automatically
Solution: Make sure you used $: before the declaration
Issue: Alert showing multiple times Solution: Check your condition logic - it may be triggering on the reset itself
Issue: Buttons not working
Solution: Verify on:click syntax is correct and function names match exactly
Issue: Limits not working for both numbers
Solution: Use || (OR) operator to check both conditions, and use separate if statements for each number's reset
Ready for more? Try these bonus features:
Beginner Extensions:
Advanced Extensions:
Creative Extensions:
../../Activities/Activity 05- Svelte Reactivity.mdxRemember: Reactivity is one of Svelte's superpowers! Unlike other frameworks where you manually update the DOM, Svelte automatically keeps your UI in sync with your data. Master this concept, and you'll write cleaner, more intuitive code.
๐ก Pro Tip: When debugging reactive code, use console.log() inside reactive statements to see when they run. This helps you understand the dependency tracking!