Hello readers! Here is a small project I’ve been working on using HTML, CSS, and JavaScript.
Step 1: Create the HTML Structure
First, let’s set up the basic HTML structure for our calculator. Create a new file called index.html
and add the following code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Simple Calculator</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="calculator">
<input type="text" class="calculator-screen" value="" disabled />
<div class="calculator-keys">
<button type="button" class="operator" value="+">+</button>
<button type="button" class="operator" value="-">-</button>
<button type="button" class="operator" value="*">×</button>
<button type="button" class="operator" value="/">÷</button>
<button type="button" value="7">7</button>
<button type="button" value="8">8</button>
<button type="button" value="9">9</button>
<button type="button" value="4">4</button>
<button type="button" value="5">5</button>
<button type="button" value="6">6</button>
<button type="button" value="1">1</button>
<button type="button" value="2">2</button>
<button type="button" value="3">3</button>
<button type="button" value="0">0</button>
<button type="button" class="decimal" value=".">.</button>
<button type="button" class="all-clear" value="all-clear">AC</button>
<button type="button" class="equal-sign" value="=">=</button>
</div>
</div>
<script src="script.js"></script>
</body>
</html>
Step 2: Style the Calculator with CSS
Next, let’s add some basic styling to make our calculator look good. Create a file called styles.css
and add the following code:
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
font-family: 'Arial', sans-serif;
}
.calculator {
border: 1px solid #ccc;
border-radius: 10px;
width: 300px;
background-color: #f9f9f9;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
.calculator-screen {
width: 100%;
height: 80px;
border: none;
background-color: #252525;
color: #fff;
font-size: 2.5rem;
text-align: right;
padding-right: 10px;
padding-left: 10px;
}
.calculator-keys {
display: grid;
grid-template-columns: repeat(4, 1fr);
gap: 10px;
padding: 20px;
}
button {
height: 60px;
border-radius: 5px;
border: none;
background-color: #e0e1e6;
font-size: 1.5rem;
}
button.operator {
background-color: #f57c00;
color: white;
}
button.equal-sign {
background-color: #3a67f5;
color: white;
grid-column: span 2;
}
button.all-clear {
background-color: #f34e4e;
color: white;
}
button:active {
background-color: #dcdcdc;
}
Step 3: Add Functionality with JavaScript
Now, let’s add the JavaScript code that will make our calculator work. Create a file called script.js
and add the following code:
const calculator = {
displayValue: '0',
firstOperand: null,
waitingForSecondOperand: false,
operator: null,
};
function updateDisplay() {
const display = document.querySelector('.calculator-screen');
display.value = calculator.displayValue;
}
updateDisplay();
const keys = document.querySelector('.calculator-keys');
keys.addEventListener('click', (event) => {
const { target } = event;
const { value } = target;
if (!target.matches('button')) {
return;
}
switch (value) {
case '+':
case '-':
case '*':
case '/':
handleOperator(value);
break;
case '=':
handleEquals();
break;
case '.':
inputDecimal(value);
break;
case 'all-clear':
resetCalculator();
break;
default:
if (Number.isInteger(parseFloat(value))) {
inputDigit(value);
}
}
updateDisplay();
});
function inputDigit(digit) {
const { displayValue, waitingForSecondOperand } = calculator;
if (waitingForSecondOperand === true) {
calculator.displayValue = digit;
calculator.waitingForSecondOperand = false;
} else {
calculator.displayValue = displayValue === '0' ? digit : displayValue + digit;
}
}
function inputDecimal(dot) {
if (!calculator.displayValue.includes(dot)) {
calculator.displayValue += dot;
}
}
function handleOperator(nextOperator) {
const { firstOperand, displayValue, operator } = calculator;
const inputValue = parseFloat(displayValue);
if (operator && calculator.waitingForSecondOperand) {
calculator.operator = nextOperator;
return;
}
if (firstOperand == null && !isNaN(inputValue)) {
calculator.firstOperand = inputValue;
} else if (operator) {
const currentValue = firstOperand || 0;
const result = performCalculation[operator](currentValue, inputValue);
calculator.displayValue = `${parseFloat(result.toFixed(7))}`;
calculator.firstOperand = result;
}
calculator.waitingForSecondOperand = true;
calculator.operator = nextOperator;
}
const performCalculation = {
'/': (firstOperand, secondOperand) => firstOperand / secondOperand,
'*': (firstOperand, secondOperand) => firstOperand * secondOperand,
'+': (firstOperand, secondOperand) => firstOperand + secondOperand,
'-': (firstOperand, secondOperand) => firstOperand - secondOperand,
};
function handleEquals() {
const { firstOperand, displayValue, operator } = calculator;
const inputValue = parseFloat(displayValue);
if (operator && firstOperand != null) {
calculator.displayValue = performCalculation[operator](firstOperand, inputValue);
calculator.firstOperand = null;
calculator.operator = null;
calculator.waitingForSecondOperand = false;
}
}
function resetCalculator() {
calculator.displayValue = '0';
calculator.firstOperand = null;
calculator.waitingForSecondOperand = false;
calculator.operator = null;
}
Step 4: Test Your Calculator
With everything set up, open your index.html
file in a web browser. You should see your calculator on the screen. Try pressing the buttons to perform calculations, and make sure everything works as expected.