-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
155 lines (130 loc) · 3.98 KB
/
main.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
let firstNumber = secondNumber = ''
let operator = ''
let answer = ''
let firstValueSet = false
let displayValue = '0'
function clickButtons() {
const buttons = document.querySelectorAll('button')
buttons.inn
buttons.forEach(button => button.addEventListener('click', function () {
if (button.classList.contains('number-button'))
setNumber(button.innerHTML)
else if (button.id === 'equals')
evaluate()
else if (button.classList.contains('operation-button'))
setOperator(button.innerHTML)
else if (button.id === 'clear')
clear()
else if (button.id === 'invert')
invert()
else if (button.id === 'percentage')
percentage()
}))
}
clickButtons()
function updateDisplay(value) {
displayValue = value
const display = document.querySelector('#display')
resizeDisplay()
display.innerHTML = displayValue
}
function resizeDisplay() {
const defaultSize = 50
const defaultLength = 7
const reduceBy = 0.5
const size = displayValue.length
const resizeBy = (reduceBy / (Math.ceil(size/ defaultLength)) * 2)
const newSize = (defaultSize * resizeBy) + 'px'
const display = document.querySelector('#display')
display.style.fontSize = newSize
}
function setNumber(value) {
if (!firstValueSet) {
if(checkPeriod(value,firstNumber)) return
firstNumber += value
updateDisplay(firstNumber)
}
else {
if((checkPeriod(value,secondNumber))) return
secondNumber += value
updateDisplay(secondNumber)
}
console.log(`firstNumber: ${firstNumber} secondNumber: ${secondNumber} operator: ${operator}`)
}
function checkPeriod(value, number) {
const isPeriod = value === '.'
const hasPeriod = number.includes('.')
return (isPeriod && hasPeriod) ? true : false
}
function setOperator(value) {
firstValueSet = true
if (operator !== '') {
evaluate()
operator = value
return
}
else if (firstNumber === '')
firstNumber = '0'
operator = value
updateDisplay(operator)
}
function evaluate() {
if (secondNumber === '') return
console.log(`firstNumber: ${firstNumber} secondNumber: ${secondNumber} operator: ${operator}`)
let firstNum = parseFloat(firstNumber)
let secondNum = parseFloat(secondNumber)
console.log(`firstNum: ${firstNum} secondNum: ${secondNum} operator: ${operator}`)
switch (operator) {
case '/':
answer = firstNum / secondNum
break
case 'x':
answer = firstNum * secondNum
break
case '-':
answer = firstNum - secondNum
break
case '+':
answer = firstNum + secondNum
break
default:
return
}
console.log(`answer: ${answer}`)
answer += ''
updateDisplay(answer)
firstNumber = answer
secondNumber = ''
operator = ''
}
function clear() {
firstNumber = secondNumber = ''
firstValueSet = false
updateDisplay('0')
}
function invert() {
let invertedVal
if (!firstValueSet && firstNumber !== '') {
(firstNumber > 0) ? invertedVal = "-" + firstNumber : invertedVal = firstNumber.substring(1)
firstNumber = invertedVal
updateDisplay(firstNumber)
}
else if (secondNumber !== '') {
(secondNumber > 0) ? invertedVal = "-" + secondNumber : invertedVal = secondNumber.substring(1)
secondNumber = invertedVal
updateDisplay(secondNumber)
}
}
function percentage() {
let percentagedVal
if (!firstValueSet && firstNumber !== '' && firstNumber !== '0'){
percentagedVal = parseFloat(firstNumber)/100
firstNumber = '' + percentagedVal
updateDisplay(firstNumber)
}
else if (secondNumber !== '' && secondNumber !== '0'){
percentagedVal = parseFloat(secondNumber)/100
secondNumber = '' + percentagedVal
updateDisplay(secondNumber)
}
}