This repository was archived by the owner on Sep 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
137 lines (105 loc) · 3.08 KB
/
Copy pathscript.js
File metadata and controls
137 lines (105 loc) · 3.08 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
class Calculator {
constructor(a, b) {
this.a = a;
this.b = b;
clearAll();
}
}
*/
const add = function(a, b) {
return a + b;
}
const subtract = function(a, b) {
return a - b;
}
const multiply = function(a, b) {
return a * b;
}
const divide = function(a, b) {
return a / b;
}
const power = function(a, b) {
let total = a ** b;
return total;
}
const factorial = function(a) {
let total = 1;
for (let i = 2; i <= a; i++) {
total *= i;
}
return total;
};
const previousDisplay = document.getElementById('previous-operand');
const numericInputs = document.querySelectorAll('input[value].data-number');
const currentDisplay = document.getElementById('current-operand');
const operationInputs = document.querySelectorAll('input[value].data-operator');
const equalsButton = document.getElementById('=');
const clearButton = document.getElementById('AC');
const deleteButton = document.getElementById('DEL');
let previousOperand = '';
let operation = '';
let currentOperand = '';
numericInputs.forEach(function(input) {
input.addEventListener("click", function() {
currentOperand += input.value;
currentDisplay.textContent = currentOperand;
});
});
operationInputs.forEach(function(input) {
input.addEventListener('click', function() {
if (operation.length !== '') {
if (previousOperand !== '') {
const result = operate(parseFloat(previousOperand), operation, parseFloat(currentOperand));
previousOperand = result;
currentOperand = '';
previousDisplay.textContent = previousOperand;
currentDisplay.textContent = '';
} else {
previousOperand = currentOperand;
currentOperand = '';
}
}
operation = input.value;
});
});
equalsButton.addEventListener('click', function() {
if (currentOperand !== '' && operation !== '') {
const result = operate(parseFloat(previousOperand), operation, parseFloat(currentOperand));
previousOperand = '';
currentOperand = result;
previousDisplay.textContent = '';
currentDisplay.textContent = currentOperand;
}
});
function clearAll() {
previousOperand = '';
operation = '';
currentOperand = '';
previousDisplay.textContent = '';
currentDisplay.textContent = '';
}
clearButton.addEventListener('click', clearAll);
function del() {
currentOperand = currentOperand.slice(0, -1);
currentDisplay.textContent = currentOperand;
}
deleteButton.addEventListener('click', del);
function operate(a, operation, b) {
switch(operation) {
case '+':
return add(a, b);
case '-':
return subtract(a, b);
case '*':
return multiply(a, b);
case "/":
return divide(a, b);
default:
return '';
}
}
// console.log(displayValue);
// const display = document.getElementById('current-operand');
// display.textContent = currentOperand;
// const calculator = new Calculator(a, b);