-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
125 lines (120 loc) · 3.49 KB
/
script.js
File metadata and controls
125 lines (120 loc) · 3.49 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
document.body.addEventListener("change", function(e){
let target = e.target;
container = document.body.querySelector(".container")
switch (target.id) {
case "firstToggle":
container.setAttribute("data-theme", "one");
break;
case "secToggle":
container.setAttribute("data-theme", "two");
break;
case "thirdToggle":
container.setAttribute("data-theme", "three")
break;
default:
return;
}
});
const calculator = {
displayNumber : '399.981',
operator : null,
firstNumber : null,
waitingForSecondNumber : false
};
const inputDigit = (digit) => {
if(calculator.waitingForSecondNumber === true){
calculator.displayNumber = digit;
calculator.waitingForSecondNumber = false;
} else {
calculator.displayNumber += digit;
}
};
const inputDecimal = (dot) => {
if (calculator.waitingForSecondNumber === true) {
calculator.displayNumber = '0.'
calculator.waitingForSecondNumber = false;
return;
}
if (!calculator.displayNumber.includes(dot)) {
calculator.displayNumber += dot;
}
};
const handleOperator = (operator) => {
if (!calculator.waitingForSecondNumber){
calculator.operator = operator;
calculator.waitingForSecondNumber = true;
calculator.firstNumber = calculator.displayNumber
// calculator.displayNumber = '0'
} else {
alert('operator already set!');
}
};
const performaCalculation = () => {
let computation
const prev = parseFloat(calculator.firstNumber)
const current = parseFloat(calculator.displayNumber)
if (isNaN(prev) || isNaN(current)) return
switch (calculator.operator) {
case '+':
computation = prev + current
break
case '-':
computation = prev - current
break
case 'x':
computation = prev * current
break
case '/':
computation = prev / current
break
default:
alert("operator doesn't already set!");
}
calculator.displayNumber = `${parseFloat(computation.toFixed(7))}`
calculator.operator = undefined
}
const resetCalculator = () => {
calculator.displayNumber = '0';
calculator.operator = null;
calculator.firstNumber = null;
calculator.waitingForSecondNumber = false
};
const deleteValue = () => {
if(calculator.displayNumber === '0'){
calculator.displayNumber = '0'
} else {
calculator.displayNumber = calculator.displayNumber.slice(0,-1);
};
};
const updateDisplay = () => {
document.querySelector(".display-number").innerHTML = calculator.displayNumber;
};
updateDisplay();
const buttons = document.querySelectorAll(".button");
for (let button of buttons){
button.addEventListener('click', function(event){
const target = event.target;
if(target.classList.contains('reset')){
resetCalculator();
updateDisplay();
return;
}
if(target.classList.contains('delete')){
deleteValue();
updateDisplay();
return;
}
if (target.classList.contains('operator')){
handleOperator(target.innerHTML);
updateDisplay()
return;
}
if (target.classList.contains('equals')) {
performaCalculation();
updateDisplay();
return;
}
inputDigit(target.innerHTML);
updateDisplay()
});
}