forked from amitrupu/Code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
calculator.html
37 lines (36 loc) · 1.25 KB
/
calculator.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<!DOCTYPE html>
<html>
<head>
<title>
Calculator
</title>
</head>
<body>
<p class="result">0</p>
<input class="input" placeholder="Enter value"><br><br>
<button onclick="calc('+')">+</button>
<button onclick="calc('-')">-</button>
<button onclick="calc('*')">*</button>
<button onclick="calc('/')">/</button>
<script>
let result = 0;
let domResult = document.querySelector(".result");
function calc(operator) {
let domInput = document.querySelector(".input");
let value = Number(domInput.value);
// console.log(result + ' ' + value);
domResult.textContent = result + ' ' + operator + ' ' + value;
if (operator === '+') {
result += value;
} else if (operator === '-') {
result -= value;
} else if (operator === '*') {
result *= value;
} else if (operator === '/') {
result /= value;
}
domResult.textContent += ' = ' + result;
}
</script>
</body>
</html>