Let's create a Calculator App! As your first big project, there will be a lot of detailed instructions that you can follow along. As you progress, you will be given fewer and fewer instructions (but more freedom) to work with until you can make an app just from a simple idea!
Demo: https://coderschool-calculator.netlify.app/
Try the calculator below, then build your own following the instructions.
You can work on an online editor like CodeSandbox or work locally with Visual Studio Code, then push it to Github. Then submit the project link to CoderSchool.
Download the assets here (if provided). We will attempt to follow a design by a UX/UI designer.
Each designer may have a different way of presenting the mockup and specifications to you. Sometimes, the design may not be very clear, and as a web developer, we need to be flexible and work with what's given to us. For this exercise, feel free to apply the specifications in your own interpretation.
Specifications:
| Item | Value |
|---|---|
| Page | Background color: #29262c, Font: Dosis, Font size: 24px (where applicable), Font weight: 700 |
| Calculator | Width: 400px (desktop) or less (mobile), Background color: #967c6d |
| Display screen | Background color: #382325 |
| Button | Colors: #d74742 and #fff, Background colors: #923532 and #fff, Height: 50px, Width: 25% of button area |
First, let's create the basic structure of your webpage.
div with id="calculator" in the body of your HTML.Add box-sizing: border-box to every element to avoid having to deal with overlapping elements and breaking margin and padding:
* {
box-sizing: border-box;
}
body {
background-color: #29262c;
}
#calculator {
width: 400px;
height: 400px;
background-color: #967c6d;
}If you set a fixed width of 400px, on mobile devices with a screen size of less than 400px, there will be a vertical scroll. Use max-width and width: 100% to make the calculator responsive, and add some margin.
#calculator {
width: 100%;
max-width: 400px;
/* remove fixed height when you add the display and buttons */
}Divide the buttons into rows. Each row consists of a maximum of 4 buttons.
Example structure:
<div class="row">
<div class="button-wrapper"><button class="button">7</button></div>
<div class="button-wrapper"><button class="button">8</button></div>
<div class="button-wrapper"><button class="button">9</button></div>
<div class="button-wrapper"><button class="button">DEL</button></div>
</div>div with id="display" where you will display the number (use a placeholder for now).To do an operation, you need to:
Note: Using eval() is not allowed in this project. Learn why.
Get the display element by its id:
let display = document.getElementById("display");Write a function that changes the display when a number button is clicked. When the user clicks 1 then 2, they should see 12, not 2 replacing 1 — so append the new digit to the current value.
You need to store the first number, the operator, and the second number:
let num1 = "";
let num2 = "";
let operator = null;num1 and num2 are strings so that when the user inputs 1 then 2, you get "12" not 3.
operator = "add" (or your chosen value).Create a function that runs when the user clicks =. Check what the operator is, then set display.innerHTML (or display.textContent) to the result of that operation. Use if/else or switch — do not use eval().
That's it! You've finished the basic calculator app. Check the requirement list and make sure to complete every required feature. Try to finish the optional ones too — pushing to the limit is how you improve fast.