-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
123 lines (108 loc) · 3.53 KB
/
Copy pathscript.js
File metadata and controls
123 lines (108 loc) · 3.53 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
const previousValueElement = document.querySelector('.previous-value')
const currentValueElement = document.querySelector('.current-value')
const clearBtn = document.querySelector('[data-clear]')
const deleteBtn = document.querySelector('[data-delete]')
const equalsBtn = document.querySelector('[data-equals]')
const digitBtns = document.querySelectorAll('[data-digit]')
const operatorBtns = document.querySelectorAll('[data-operator]')
class Calculator {
constructor(previousValueElement, currentValueElement) {
this.previousValueElement = previousValueElement
this.currentValueElement = currentValueElement
this.clear()
}
clear() {
this.operator = undefined
this.previousValue = ''
this.currentValue = ''
}
delete() {
this.currentValue = this.currentValue.toString().slice(0, -1)
}
appendNumber(number) {
if (number === '.' && this.currentValue.includes('.')) return;
this.currentValue = this.currentValue.toString() + number.toString()
}
selectOperation(operator) {
if (this.previousValue !== '') {
this.compute()
}
this.operator = operator
this.previousValue = this.currentValue
this.currentValue = ''
}
compute() {
let result;
const prev = parseFloat(this.previousValue)
const current = parseFloat(this.currentValue)
if (isNaN(prev) || isNaN(current)) return
switch (this.operator) {
case '+':
result = prev + current;
break;
case '-':
result = prev - current;
break;
case '*':
result = prev * current;
break;
case '/':
result = prev / current;
break;
default:
return;
}
this.currentValue = result
this.operator = undefined
this.previousValue = ''
}
formatNumber(number) {
const stringNumber = number.toString()
const integerDigits = parseFloat(stringNumber.split('.')[0])
const decimalDigits = stringNumber.split('.')[1]
let integerDisplay;
if (isNaN(integerDigits)) {
integerDisplay = ''
} else {
integerDisplay = integerDigits.toLocaleString()
}
if (!isNaN(decimalDigits)) {
return `${integerDisplay}.${decimalDigits}`
} else {
return integerDisplay;
}
}
updateDisplay() {
if (this.operator != null) {
this.previousValueElement.innerText = `${this.formatNumber(this.previousValue)} ${this.operator}`
} else {
this.previousValueElement.innerText = ''
}
this.currentValueElement.innerText = this.formatNumber(this.currentValue)
}
}
const calculator = new Calculator(previousValueElement, currentValueElement)
digitBtns.forEach(btn => {
btn.addEventListener('click', () => {
calculator.appendNumber(btn.innerText)
calculator.updateDisplay()
})
})
operatorBtns.forEach(btn => {
btn.addEventListener('click', () => {
calculator.selectOperation(btn.innerText)
calculator.updateDisplay()
})
})
equalsBtn.addEventListener('click', () => {
calculator.compute()
calculator.updateDisplay()
})
clearBtn.addEventListener('click', () => {
calculator.clear()
calculator.updateDisplay()
})
deleteBtn.addEventListener('click', () => {
calculator.delete()
calculator.updateDisplay()
})