Calculator In HTML< CSS < JavaScript | Project Series Day - 6 | Frontend everything
Hello Everyone 👋Welcome to My New Blog. Today I have made a Calculator with the help of HTML, CSS, and Javascript | It is Project Series Day 6. 😍
I am Piyush, Sharing About Web development Daily. You can also check out me at @frontendeverything.
Let's start making this simple calculator step by step
Video Preview of the project,
So that was the preview now let us start making the project 😄 First, we will code HTML then CSS and javascript, and also I have shared codepen ink to make it easier for you.
HTML 🎈( step - 1)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="style.css">
<script src="script.js" defer></script>
<title>Calculator in JS</title>
</head>
<body>
<div id="calculator-wrapper">
<div class="output">
<div data-previous-operand class="previous-operand"></div>
<div data-current-operand class="current-operand"></div>
</div>
<button data-clear class="span-two">CLR</button>
<button data-delete>DEL</button>
<button data-operation>÷</button>
<button data-number>1</button>
<button data-number>2</button>
<button data-number>3</button>
<button data-operation>*</button>
<button data-number>4</button>
<button data-number>5</button>
<button data-number>6</button>
<button data-operation>+</button>
<button data-number>7</button>
<button data-number>8</button>
<button data-number>9</button>
<button data-operation>-</button>
<button data-number>.</button>
<button data-number>0</button>
<button data-equals class="span-two">=</button>
</div>
</body>
</html>
So that was the HTML coding. Now we will code CSS
CSS 🎈( step - 2)
* {
box-sizing: border-box;
margin: 0;
padding:0;
}
body {
font-family: arial;
background: #4287f5;
}
#calculator-wrapper {
display : grid;
min-height: 100vh;
justify-content: center;
align-content: center;
grid-template-columns : repeat(4 ,80px);
grid-template-rows: minmax(120px,auto) repeat(5, 80px);
}
#calculator-wrapper > button {
cursor:pointer;
outline: none;
font-size: 1.8rem;
border: 2px solid #0000;
}
#calculator-wrapper>button:hover {
background-color: #ddd;
}
.span-two {
grid-column: span 2;
}
.output {
grid-column: 1 /-1;
background: #bdbdbd;
color: #000;
display:flex;
flex-direction:column;
align-items: flex-end;
justify-content: space-around;
padding: 1rem;
word-wrap: break-word;
word-break: break-all;
}
.output .previous-operand {
color: #000;
font-size:1.2rem;
}
.output .current-operand {
color: #000;
font-size: 2rem;
}
CSS coding is also done. Now let us move to JAVASCRIPT 😎
Javascript 🎈( step - 3)
//Storing information
class Calculator {
constructor(previousOpTextEl, currentOpTextEl) {
this.previousOpTextEl = previousOpTextEl;
this.currentOpTextEl = currentOpTextEl;
this.reset = false;
this.clear();
}
//clear method for clear the screen
clear(){
this.currentOperand = "";
this.previousOperand = "";
this.operation = undefined
}
//removing a single number
delete(){
this.currentOperand = this.currentOperand.toString().slice(0 ,-1);
}
//appending the number to the screen
appendNumber(number){
// if ((this.currentOperand.includes(".")) && number ==="." ) return;
if (number === "." &&(this.currentOperand.includes("."))) return;
this.currentOperand = this.currentOperand.toString() + number.toString();
}
//chooseOperation
chooseOperation(operation){
if(this.currentOperand === "") return;
if(this.previousOperand !== "") {
this.compute();
}
this.operation = operation;
this.previousOperand = this.currentOperand;
this.currentOperand="";
}
//compute the values and compute a single value/result to display on the calculator
compute(){
let computedResult;
const prev = parseFloat(this.previousOperand);
const current = parseFloat(this.currentOperand);
if(isNaN(prev) || isNaN(current)) return;
switch(this.operation) {
case '+':
computedResult = prev + current ;
break;
case '-':
computedResult = prev - current;
break;
case 'x':
computedResult = prev * current;
break;
case '÷':
computedResult = prev / current;
break;
default:
return;
}
this.reset = true;
this.currentOperand = computedResult;
this.operation = undefined;
this.previousOperand = "";
}
getDisplayNumber(number) {
const stringNumber = number.toString()
const integerDigits = parseFloat(stringNumber.split('.')[0])
const decimalDigits = stringNumber.split('.')[1]
let integerDisplay;
if (isNaN(integerDigits)) {
integerDisplay = ''
} else {
integerDisplay = integerDigits.toLocaleString('en', { maximumFractionDigits: 0 })
}
if (decimalDigits != null) {
return `${integerDisplay}.${decimalDigits}`
} else {
return integerDisplay
}
}
//updates values inside the output container
updateDisplay(){
this.currentOpTextEl.innerText = this.getDisplayNumber(this.currentOperand);
// this.previousOpTextEl.innerText = this.previousOperand;
if(this.operation != null) {
this.previousOpTextEl.innerText =
`${this.getDisplayNumber(this.previousOperand)} ${this.operation}`;
} else {
this.previousOpTextEl.innerText = ""
}
}
}
//Global Vars
const numberBtns = document.querySelectorAll("[data-number]");
const operationBtns = document.querySelectorAll("[data-operation]");
const equalsBtn = document.querySelector("[data-equals]");
const clearBtn = document.querySelector("[data-clear]");
const deleteBtn = document.querySelector("[data-delete]");
const previousOpTextEl = document.querySelector("[data-previous-operand]");
const currentOpTextEl = document.querySelector("[data-current-operand]");
//Creat a claculator
const calculator = new Calculator(previousOpTextEl, currentOpTextEl);
numberBtns.forEach(button => {
button.addEventListener("click" ,()=> {
if(calculator.previousOperand === "" && calculator.currentOperand !=="" &&calculator.reset){
calculator.currentOperand = "";
calculator.reset = false;
}
calculator.appendNumber(button.innerText);
calculator.updateDisplay();
})
})
operationBtns.forEach(button => {
button.addEventListener("click", () => {
calculator.chooseOperation(button.innerText);
calculator.updateDisplay();
})
})
equalsBtn.addEventListener("click", button => {
calculator.compute();
calculator.updateDisplay();
})
clearBtn.addEventListener("click", button => {
calculator.clear();
calculator.updateDisplay();
})
deleteBtn.addEventListener("click", button => {
calculator.delete();
calculator.updateDisplay();
})
All the coding part is done, now let us see the final output, and also below I have mentioned the codepen link.
Final Output
The codepen link is here for making your work easier!
.
.
.
.
.
.
Thank You For Scrolling Till here 😊. If You gain any knowledge then do checkout me at @frontendeverything. I am Piyush 🎉 I provide Content related to programming, technology, web development Daily.
.