-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cal.js
162 lines (140 loc) · 4.62 KB
/
Cal.js
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// DOM Elements
const valueEl = document.querySelector('.value');
const acEl = document.querySelector('.ac');
const pmEl = document.querySelector('.pm');
const percentEl = document.querySelector('.percent');
const additionEl = document.querySelector('.addition');
const subtractionEl = document.querySelector('.subtraction');
const multiplicationEl = document.querySelector('.multiplication');
const divisionEl = document.querySelector('.division');
const equalEl = document.querySelector('.equal');
const decimalEl = document.querySelector('.decimal');
const number0El = document.querySelector('.number-0');
const number1El = document.querySelector('.number-1');
const number2El = document.querySelector('.number-2');
const number3El = document.querySelector('.number-3');
const number4El = document.querySelector('.number-4');
const number5El = document.querySelector('.number-5');
const number6El = document.querySelector('.number-6');
const number7El = document.querySelector('.number-7');
const number8El = document.querySelector('.number-8');
const number9El = document.querySelector('.number-9');
const numberElArray = [
number0El, number1El, number2El, number3El, number4El,
number5El, number6El, number7El, number8El, number9El
];
// variables
let valueStrInMemory = null;
let operatorInMemory = null;
// Functions
const getValueAsStr = () => valueEl.textContent.split(',').join('');
const getValueAsNum = () => {
return parseFloat(getValueAsStr());
};
const setStrAsValue = (valueStr) => {
if (valueStr[valueStr.length - 1] === '.') {
valueEl.textContent += '.';
return;
}
const [wholeNumStr, decimalStr] = valueStr.split('.');
if (decimalStr) {
valueEl.textContent =
parseFloat(wholeNumStr).toLocaleString() + '.' + decimalStr;
} else {
valueEl.textContent = parseFloat(wholeNumStr).toLocaleString();
}
};
const handleNumberClick = (numStr) => {
const currentValueStr = getValueAsStr();
if (currentValueStr === '0') {
setStrAsValue(numStr);
} else {
setStrAsValue(currentValueStr + numStr);
}
};
const getResultOfOperationAsStr = () => {
const currentValueNum = getValueAsNum();
const valueNumInMemory = parseFloat(valueStrInMemory);
let newValueNum;
if (operatorInMemory === 'addition') {
newValueNum = valueNumInMemory + currentValueNum;
} else if (operatorInMemory === 'subtraction') {
newValueNum = valueNumInMemory - currentValueNum;
} else if (operatorInMemory === 'multiplication') {
newValueNum = valueNumInMemory * currentValueNum;
} else if (operatorInMemory === 'division') {
newValueNum = valueNumInMemory / currentValueNum;
}
return newValueNum.toString();
};
const handleOperatorClick = (operation) => {
const currentValueStr = getValueAsStr();
if (!valueStrInMemory) {
valueStrInMemory = currentValueStr;
operatorInMemory = operation;
setStrAsValue('0');
return;
}
valueStrInMemory = getResultOfOperationAsStr();
operatorInMemory = operation;
setStrAsValue('0');
};
// Add Event Listeners to functions
acEl.addEventListener('click', () => {
setStrAsValue('0');
valueStrInMemory = null;
operatorInMemory = null;
});
pmEl.addEventListener('click', () => {
const currentValueNum = getValueAsNum();
const currentValueStr = getValueAsStr();
if (currentValueStr === '-0') {
setStrAsValue('0');
return;
}
if (currentValueNum >= 0) {
setStrAsValue('-' + currentValueStr);
} else {
setStrAsValue(currentValueStr.substring(1));
}
});
percentEl.addEventListener('click', () => {
const currentValueNum = getValueAsNum();
const newValueNum = currentValueNum / 100;
setStrAsValue(newValueNum.toString());
valueStrInMemory = null;
operatorInMemory = null;
});
// add event listeners to operators
additionEl.addEventListener('click', () => {
handleOperatorClick('addition');
});
subtractionEl.addEventListener('click', () => {
handleOperatorClick('subtraction');
});
multiplicationEl.addEventListener('click', () => {
handleOperatorClick('multiplication');
});
divisionEl.addEventListener('click', () => {
handleOperatorClick('division');
});
equalEl.addEventListener('click', () => {
if (valueStrInMemory) {
setStrAsValue(getResultOfOperationAsStr());
valueStrInMemory = null;
operatorInMemory = null;
}
});
// Add Event Listeners to numbers and decimal
for (let i=0; i < numberElArray.length; i++) {
const numberEl = numberElArray[i];
numberEl.addEventListener('click', () => {
handleNumberClick(i.toString());
});
}
decimalEl.addEventListener('click', () => {
const currentValueStr = getValueAsStr();
if (!currentValueStr.includes('.')) {
setStrAsValue(currentValueStr + '.');
}
});