-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
78 lines (66 loc) · 2.45 KB
/
index.html
File metadata and controls
78 lines (66 loc) · 2.45 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Calculator</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="calculator">
<input id="display" type="text" readonly placeholder="0">
<div class="buttons-container">
<button onclick="clearDisplay()">C</button>
<button onclick="addDigit('(')">(</button>
<button onclick="addDigit(')')">)</button>
<button class="operation" onclick="changeOperator('/')">/</button>
<button onclick="addDigit('1')">1</button>
<button onclick="addDigit('2')">2</button>
<button onclick="addDigit('3')">3</button>
<button class="operation" onclick="changeOperator('+')">+</button>
<button onclick="addDigit('4')">4</button>
<button onclick="addDigit('5')">5</button>
<button onclick="addDigit('6')">6</button>
<button class="operation" onclick="changeOperator('-')">-</button>
<button onclick="addDigit('0')">0</button>
<button onclick="addDigit('.')">.</button>
<button class="equals" onclick="calculateResult()">=</button>
</div>
</div>
<script>
let operator = '';
let num1 = '0';
let num2 = '';
// Let's us know if we are inputting the num2 (after operator) or num1 (before operator)
let afterOperator = false;
function addDigit(val) {
if (afterOperator){
num2 += val;
document.getElementById('display').value = num2;
} else {
num1 = (num1 == '0') ? val : num1 + val;
document.getElementById('display').value = num1;
}
}
function changeOperator(val) {
operator = val;
calculateResult()
afterOperator = true;
}
function clearDisplay() {
operator = ''
num1 = '0'
num2 = ''
afterOperator = false;
document.getElementById('display').value = '';
}
function calculateResult() {
if (num1=='' || num2=='') return;
num1 = 0; // EDIT THIS
num2 = '';
document.getElementById('display').value = num1;
afterOperator = false;
}
</script>
</body>
</html>