-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
205 lines (184 loc) · 4.16 KB
/
script.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
let firstValue;
let operend;
let nextValue;
let displayValue = '';
let result;
const allClearButton = document.querySelector('.ac')
const clear = document.querySelector('.c')
const displayCalculation = document.querySelector('.calculation')
const displayResult = document.querySelector('.result')
const digits = document.querySelectorAll('.digit')
const operators = document.querySelectorAll('.operator')
const equal = document.querySelector('#equals')
const dotButton = document.querySelector('.dot')
digits.forEach((digit) => {
digit.addEventListener('click',()=>{
if(displayResult.textContent == 0){
displayResult.textContent = ''
}
appendDigit(digit.outerText)
displayScreen()
})
})
function operatorKeys(e){
if(!firstValue){
firstValue = displayValue
operend = e.key
displayValue = ''
}
else{
evaluate()
displayValue = ''
operend = e.key
}
}
operators.forEach((operator) => {
operator.addEventListener('click', ()=> {
if(!firstValue){
firstValue = displayValue
operend = operator.innerText
displayCalculationScreen()
displayValue = ''
}
else{
evaluate()
displayValue = ''
operend = operator.innerText
displayCalculationScreen()
}
})
})
function add(a,b){
a = Number(`${a}`)
b = Number(`${b}`)
if(Number.isInteger(a) && Number.isInteger(b)){
result = parseInt(a)+parseInt(b)
return(result)
}
else{
result = parseFloat(a)+parseFloat(b)
return(result.toFixed(2))
}
}
function subtract(a,b){
result = a - b
if(!Number.isInteger(result)){
return result.toFixed(2)
}
else{
return(result)
}
}
function multiply(a,b){
result = a*b
if(!Number.isInteger(result)){
return result.toFixed(2)
}
else{
return(result)
}
}
function divide(a,b){
result = a / b
if(!Number.isInteger(result)){
return result.toFixed(2)
}
else{
return(result)
}
}
function operate(a,o,b){
if(o == '+'){
return add(a,b)
}
else if(o == '-'){
return subtract(a,b)
}
else if(o == '/'){
return divide(a,b)
}
else if(o == '*'){
return multiply(a,b)
}
else{
return;
}
}
function backSpace(){
displayValue = displayValue.slice(0,-1)
displayScreen()
if(displayValue.length == 0){
return;
}
}
clear.addEventListener('click',backSpace)
allClearButton.addEventListener('click',() => {
displayValue = '0'
firstValue = null
nextValue = null
displayResult.textContent = displayValue
displayCalculation.textContent = ''
})
function appendDigit(element){
if(displayValue[0] == 0){
displayValue = ''
}
displayValue += `${element}`
}
function enter(){
if(!firstValue){
return;
}
else{
evaluate()
firstValue = null
}
}
equal.addEventListener('click', enter)
function displayScreen(){
displayResult.textContent = displayValue
}
function displayCalculationScreen(){
displayCalculation.textContent = `${firstValue}`+' '+`${operend}`
}
function evaluate(){
if(!nextValue){
nextValue = displayValue
displayValue = operate(firstValue,operend,nextValue)
displayCalculation.textContent = `${firstValue}`+' '+`${operend}`+' '+`${nextValue}`+' '+'='+' '
displayScreen()
firstValue = displayValue
nextValue = null
}
}
window.addEventListener('keydown', (e)=> {
if(e.key >= 0 && e.key <= 9){
appendDigit(e.key)
displayScreen()
}
else if(e.key == '.'){
dot()
}
else if(e.key == 'Backspace'){
backSpace()
}
else if(e.key == 'Enter'){
enter()
}
else if(e.key == '*' || e.key == '-' || e.key == '+' || e.key == '/' ){
operatorKeys(e)
}
else{
return;
}
})
function dot(){
if(!displayValue.includes('.')){
appendDigit('.')
displayScreen()
}
else{
return;
}
}
dotButton.addEventListener('click',dot)