Simple Javascript Calculator || Calculator using javascript || HTML || CSS || Javascript

preview_player
Показать описание
"Learn how to create a simple calculator using JavaScript, HTML, and CSS. This step-by-step tutorial is perfect for beginners looking to enhance their web development skills. Build a fully functional calculator with basic operations like addition, subtraction, multiplication, and division. Let's dive into coding and create a calculator together!"

Simple Javascript Calculator || Calculator using javascript || HTML || CSS || Javascript

Topics that covered-
1- javascript calculator
2 - calculator using javascript
3- javascript
4- javascript calculator tutorial
5 - simple calculator using javascript
6 - calculator using html css javascript
7 - how to build a simple calculator using javascript
8 - how to build a calculator in javascript
9 - javascript calculator project
10 - calculator,build a calculator in javascript
11- javascript tutorial
12 - how to create calculator using javascript
13 - make calculator using javascript
14 - javascript simple calculator tutorial
Рекомендации по теме
Комментарии
Автор

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Calculator</title>
<style>
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
background-color: #333;
}

.calculator {
background-color: #f3f3f3;
padding: 20px;
border-radius: 10px;
box-shadow: 0px 0px 20px 5px rgba(0, 0, 0, 0.2);
}

#display {
width: 100%;
height: 50px;
font-size: 24px;
margin-bottom: 20px;
text-align: right;
padding-right: 10px;
border: none;
background-color: #ddd;
border-radius: 5px;
}

.buttons {
display: grid;
grid-template-columns: repeat(4, 1fr);
gap: 10px;
}

button {
height: 50px;
font-size: 18px;
background-color: #eee;
border: none;
border-radius: 5px;
cursor: pointer;
transition: background-color 0.3s;
}

button:hover {
background-color: #ccc;
}

.operator {
background-color: #333;
color: #fff;
}

.clear {
background-color: #e74c3c;
color: white;
}

.equals {
background-color: #333;
color: white;
}

</style>>
</head>
<body>
<div class="calculator">
<input type="text" id="display" disabled>
<div class="buttons">
<button onclick="clearDisplay()" class="clear">C</button>
<button
<button
<button
<button onclick="chooseOperator('+')" class="operator">+</button>

<button
<button
<button
<button onclick="chooseOperator('-')" class="operator">-</button>

<button
<button
<button
<button onclick="chooseOperator('*')" class="operator">x</button>

<button
<button onclick="calculate()" class="equals">=</button>
<button onclick="chooseOperator('/')" class="operator">/</button>
</div>
</div>
<script>
let display =
let currentOperator = null;
let firstOperand = '';
let secondOperand = '';

function clearDisplay() {
display.value = '';
firstOperand = '';
secondOperand = '';
currentOperator = null;
}

function appendNumber(number) {
if (currentOperator === null) {
firstOperand += number;
display.value = firstOperand;
} else {
secondOperand += number;
display.value = secondOperand;
}
}

function chooseOperator(operator) {
if (firstOperand === '') return;
currentOperator = operator;
display.value = '';
}

function calculate() {
if (currentOperator === null || secondOperand === '') return;
let result;
const a = parseFloat(firstOperand);
const b = parseFloat(secondOperand);
switch (currentOperator) {
case '+':
result = a + b;
break;
case '-':
result = a - b;
break;
case '*':
result = a * b;
break;
case '/':
result = a / b;
break;
default:
return;
}
display.value = result;
firstOperand = result.toString();
secondOperand = '';
currentOperator = null;
}

</script>
</body>
</html>

mca_waala