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 8
Build an interactive math calculator using Svelte's reactivity system. This project teaches you how to handle user input, perform calculations, implement multiple functions, and manage application state - all core concepts for building dynamic web applications.
By completing this project, you will:
bind:value
on:click
โ Basic SvelteKit project structure โ Development environment configuration โ Variable declarations (firstNumber, secondNumber, result) โ Empty function stubs with hints โ Complete UI structure (inputs, buttons, result display) โ Bootstrap CSS framework
Input Binding
firstNumber
to first input fieldsecondNumber
to second input fieldbind:value
Arithmetic Operations (5 functions)
addition()
- Add two numberssubtraction()
- Subtract second from firstmultiplication()
- Multiply two numbersdivision()
- Divide with zero-checkmodulo()
- Find remainderEvent Handling
on:click
Error Handling
Result Display
Navigate to this template folder:
cd "Paid Courses/W3 Server-Side Development & Authentication/Templates/project-02-calculator"
Install dependencies:
npm install
Start development server:
npm run dev
Open in browser:
Visit http://localhost:5173
project-02-calculator/
โโโ src/
โ โโโ routes/
โ โโโ +page.svelte # Main calculator (TODO: implement logic)
โโโ package.json # Dependencies
โโโ svelte.config.js # Svelte configuration
โโโ vite.config.js # Build configuration
Before implementing, research and answer:
Two-Way Binding: What is bind:value
in Svelte?
Event Handlers: How do you handle clicks in Svelte?
on:click
?Reactivity: How does Svelte's reactivity work?
Math Operations: What are JavaScript's math operators?
File: src/routes/+page.svelte
<!-- Current: -->
<input type="number" class="form-control" placeholder="First Number" />
<!-- TODO: Add bind:value -->
<input
type="number"
class="form-control"
placeholder="First Number"
bind:value={firstNumber}
/>
<!-- Repeat for secondNumber -->
What to learn:
bind:value
creates two-way binding<script>
function addition() {
result = firstNumber + secondNumber;
}
</script>
What to learn:
<!-- Current: -->
<button class="btn btn-primary btn-block">+ Add</button>
<!-- TODO: Add on:click -->
<button class="btn btn-primary btn-block" on:click={addition}>
+ Add
</button>
What to learn:
on:click={functionName}
attaches event<script>
function division() {
if (secondNumber === 0) {
result = "Error: Cannot divide by zero";
} else {
result = firstNumber / secondNumber;
}
}
</script>
What to learn:
<!-- Current: -->
<p class="text-center">Result: </p>
<!-- TODO: Show result variable -->
<p class="text-center">Result: {result}</p>
What to learn:
Your project is complete when:
on:click
handlersTest these scenarios:
Basic Operations:
Edge Cases:
Reactivity:
UI Responsiveness:
Criteria | Points | Description |
---|---|---|
Functions | 40 | All 5 operations implemented correctly |
Reactivity | 25 | Proper use of bind:value and reactive updates |
Error Handling | 20 | Division by zero and edge cases handled |
UI/UX | 15 | Clean interface, responsive buttons, result display |
Total | 100 |
Issue: Input not updating variable
Solution: Check bind:value={variableName}
syntax is correct
Issue: Button click does nothing
Solution: Verify on:click={functionName}
is attached (no parentheses)
Issue: Result not displaying
Solution: Ensure you have {result}
in the result display area
Issue: Calculator always shows 0
Solution: Check that functions assign to result
variable
Issue: Division by zero crashes app
Solution: Add if-statement to check secondNumber === 0
before dividing
Enhance your calculator with custom styles:
:global(body) {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
min-height: 100vh;
}
.calculator-container {
max-width: 600px;
margin: 3rem auto;
padding: 2rem;
background: white;
border-radius: 20px;
box-shadow: 0 20px 60px rgba(0,0,0,0.3);
}
.result-display {
font-size: 2rem;
font-weight: bold;
color: #667eea;
margin-top: 2rem;
padding: 1rem;
background: #f7fafc;
border-radius: 10px;
}
When ready to deploy:
npm run build
npm run preview # Test production build locally
../../Project/Project 02- Math Calculator.mdx
Remember: Reactivity is Svelte's superpower! Unlike other frameworks, you don't need complex state management - just assign values and Svelte handles the rest.
๐ก Pro Tip: Start with one function (addition), get it working perfectly, then copy the pattern to the others. This "build one, replicate many" approach is a professional development strategy.