Apply your knowledge to build something amazing!
:information_source: Info Project Overview
graph LR
A[🎯 Project Start] --> B[📝 Setup Variables]
B --> C[➕ Add Function]
C --> D[➖ Subtract Function]
D --> E[✖️ Multiply Function]
E --> F[➗ Divide Function]
F --> G[🎨 Create UI]
G --> H[💅 Add Styling]
H --> I[🛡️ Error Handling]
I --> J[🎉 Project Complete!]
Mini Project Guidelines
In this exciting lesson, we'll be creating a fully functional Math Calculator that performs basic operations (add, subtract, multiply, divide). By the end, you'll have a calculator that looks professional and works perfectly!
Here are the design examples from Google:
To get the SvelteKit project template, click on the link below:
https://glitch.com/edit/#!/breezy-hypnotic-coyote
:bulb: Getting Started
- Click the link above to open the project template
- Click "Remix" to create your own copy
- Wait for the project to load completely
- Open the
+page.svelte
file to start coding!
We'll create four functions to handle our math operations:
:warning: Common Pitfall #1 Make sure you convert input values to numbers! By default, HTML inputs return strings, so "2" + "3" would give you "23" instead of 5.
First, let's create the necessary variables to store our calculator's data:
let firstNumber = 0;
let secondNumber = 0;
let result = 0;
:bulb: Why Initialize to 0? We set our variables to 0 initially so they have a valid number value from the start. This prevents errors and gives users a clean starting point!
:dart: Milestone One: Variables created! You now have storage for your calculator's numbers.
Now, let's create our first mathematical operation - addition:
function addition() \{
result = firstNumber + secondNumber;
\}
What's happening here? When this function runs, it takes firstNumber
, adds secondNumber
to it, and stores the answer in result
.
Next, let's create a subtraction function:
function subtraction() \{
result = firstNumber - secondNumber;
\}
Remember: This subtracts the second number FROM the first number. So if firstNumber is 10 and secondNumber is 3, the result will be 7.
Time for multiplication! This function will multiply our two numbers:
function multiplication() \{
result = firstNumber * secondNumber;
\}
:bulb: Fun Fact In programming, we use
*
for multiplication instead of×
because it's easier to type on a keyboard!
Finally, let's create our division function:
function division() \{
result = firstNumber / secondNumber;
\}
:warning: Common Pitfall #2 What happens if someone tries to divide by zero? We'll fix this later with error handling!
:dart: Milestone 2: All math functions created! Your calculator can now perform all four basic operations.
Now comes the exciting part - creating the user interface! This is what users will see and interact with:
<div class="container">
<h1>Math Calculator</h1>
<div class="input-section">
<label for="first">First Number:</label>
<input id="first" bind:value=\{firstNumber\} type="number" />
<label for="second">Second Number:</label>
<input id="second" bind:value=\{secondNumber\} type="number" />
</div>
<div class="button-section">
<button on:click=\{addition\}>Add (+)</button>
<button on:click=\{subtraction\}>Subtract (-)</button>
<button on:click=\{multiplication\}>Multiply (×)</button>
<button on:click=\{division\}>Divide (÷)</button>
</div>
<div class="result-section">
<h2>Result: \{result\}</h2>
</div>
</div>
:bulb: Understanding the Code
bind:value=\{firstNumber\}
creates a two-way connection between the input and our variableon:click=\{addition\}
tells the button to run our addition function when clickedtype="number"
ensures users can only input numbers
:dart: Milestone 3: User interface complete! Your calculator now has a way for users to interact with it.
Time to make your calculator look amazing! Let's add some professional CSS styling:
<style>
.container \{
max-width: 400px;
margin: 50px auto;
padding: 20px;
border: 1px solid #ddd;
border-radius: 10px;
text-align: center;
font-family: Arial, sans-serif;
\}
.input-section \{
margin: 20px 0;
\}
.input-section label \{
display: block;
margin: 10px 0 5px;
font-weight: bold;
\}
.input-section input \{
width: 100%;
padding: 10px;
border: 1px solid #ccc;
border-radius: 5px;
font-size: 16px;
\}
.button-section \{
margin: 20px 0;
\}
.button-section button \{
margin: 5px;
padding: 10px 20px;
background-color: #007bff;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
font-size: 14px;
\}
.button-section button:hover \{
background-color: #0056b3;
\}
.result-section \{
margin: 20px 0;
padding: 15px;
background-color: #f8f9fa;
border-radius: 5px;
\}
.result-section h2 \{
margin: 0;
color: #28a745;
\}
</style>
:bulb: Styling Best Practices
- The
border-radius
property gives us smooth, rounded corners- The
:hover
effect makes buttons darker when you hover over them- Using
margin: auto
centers our calculator on the page- Green color for results makes them stand out positively!
:dart: Milestone 4: Your calculator now looks professional and is easy to use!
Great job getting this far! Now let's add some advanced features to make your calculator even better.
Remember our warning about dividing by zero? Let's fix that now:
function division() \{
if (secondNumber === 0) \{
result = "Error: Cannot divide by zero";
\} else \{
result = firstNumber / secondNumber;
\}
\}
:warning: Common Pitfall #3 Always check for division by zero! It's mathematically impossible and will cause errors in your program.
Let's add a helpful clear button to reset everything:
function clear() \{
firstNumber = 0;
secondNumber = 0;
result = 0;
\}
Don't forget to add the button to your HTML:
<button on:click=\{clear\}>Clear</button>
:bulb: User Experience Tip A clear button is essential for good calculator design. Users make mistakes and need an easy way to start over!
:dart: Milestone 5: Advanced features complete! Your calculator now handles errors gracefully and has a clear function.
Having trouble? Here are common issues and how to fix them:
:information_source: Info Common Issues & Solutions
"My result shows NaN (Not a Number)"
type="number"
on your inputs"Clicking buttons doesn't do anything"
on:click
not onClick
"My styling doesn't appear"
<style>
tags are in the right place"Division gives weird decimal numbers"
result.toFixed(2)
to limit to 2 decimal placesReady to take your calculator to the next level? Try these challenges:
Add M+, M-, and MR (Memory Recall) buttons:
let memory = 0;
function memoryAdd() \{
memory += result;
\}
function memoryClear() \{
memory = 0;
\}
Keep track of the last 5 calculations:
let history = [];
function addToHistory(operation) \{
history = [`$\{firstNumber\} $\{operation\} $\{secondNumber\} = $\{result\}`, ...history].slice(0, 5);
\}
Add keyboard shortcuts for operations:
function handleKeypress(event) \{
if (event.key === '+') addition();
if (event.key === '-') subtraction();
if (event.key === '*') multiplication();
if (event.key === '/') division();
if (event.key === 'c') clear();
\}
Add advanced operations like square root, power, and percentage!
Math Calculator (Code Review) https://youtu.be/example-math-calculator
Want to enhance your calculator with AI assistance? Try these prompts to take your project further:
:bulb: Using AI Effectively When using AI prompts, always:
- Test the generated code thoroughly
- Understand what each line does
- Modify it to fit your specific needs
Helpful Prompts to Try:
Congratulations on completing your Math Calculator! :tada: You've learned so much:
:warning: Final Checklist
- All four operations (add, subtract, multiply, divide) work correctly
- Division by zero shows an error message
- The calculator has professional styling
- Clear button resets all values
- You've tested with different numbers
:bulb: Pro Tip Take a screenshot of your working calculator to share with friends and family. You built this from scratch - be proud of your achievement!
Remember: Every professional developer started with projects just like this one. Keep practicing, keep building, and keep learning! :rocket: