-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
163 lines (131 loc) · 4.57 KB
/
index.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
const $calculator = document.querySelector('.form')
const $controls = $calculator.querySelector('.form-controls')
const numbers = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '.']
const operators = ['+', '-', '*', '/', '%']
const controls = ['CE', 'C', '=']
let $buttonNumber = ''
let $buttonOperators = ''
let $buttonControls = ''
numbers.forEach((number) => {
$buttonNumber += '<button class="form-number" >' + number + '</button>'
})
operators.forEach((operator) => {
$buttonOperators += '<button class="form-operator" >' + operator + '</button>'
})
controls.forEach((control) => {
$buttonControls += '<button class="form-control">' + control + '</button>'
})
$controls.insertAdjacentHTML('beforeend', $buttonNumber)
$controls.insertAdjacentHTML('beforeend', $buttonOperators)
$controls.insertAdjacentHTML('beforeend', $buttonControls)
const $buttons = $calculator.querySelectorAll('button')
const $display = $calculator.querySelector('.form-display')
const $expresion = $display.querySelector('.display-expresion')
const $currentValue = $display.querySelector('.display-value')
let digit = '0'
let expression = ''
$buttons.forEach(($button) => {
$button.addEventListener('click', (e) => {
e.preventDefault()
if (e.target.classList.contains('form-number')) {
digit = $currentValue.value + e.target.textContent
handleChangeInput(digit)
}
if (e.target.classList.contains('form-operator')) {
digit = $currentValue.value
const operator = e.target.textContent
const currentValue = digit
expression += currentValue + operator
handleOperator({ currentValue, operator })
}
if (e.target.textContent == 'CE') {
handleClearClick()
digit = ''
expression = ''
}
if (e.target.textContent == 'C') {
handleRemoveClick()
}
if (e.target.textContent == '=') {
if(document.querySelector('.expresion-equal')) return
expression += $currentValue.value
const currentValue = $currentValue.value
$expresion.insertAdjacentHTML(
'beforeend',
`<span>${currentValue}</span>
<span class='expresion-equal'> = </span>`
)
$currentValue.value = handleResolveClick(expression)
expression = ''
}
})
})
function handleChangeInput(number) {
$currentValue.value = number
const hasOperator = operators.some(operator => number.includes(operator))
const hasEqual = $currentValue.value.includes('=')
const isDecimal = $currentValue.value.includes('.')
if (hasOperator && !hasEqual) {
const operator = number.slice(-1)
const currentValue = number.slice(0, -1)
$currentValue.value = currentValue
expression += currentValue + operator
handleOperator({ currentValue, operator })
}
if (!isDecimal && !hasEqual) {
const formatedDigit = + $currentValue.value
$currentValue.value = formatedDigit
}
if(hasEqual){
$currentValue.value = number.slice(0, -1)
if(document.querySelector('.expresion-equal')) return
expression += $currentValue.value
$expresion.insertAdjacentHTML(
'beforeend',
`<span>${$currentValue.value}</span>
<span class='expresion-equal'> = </span>`
)
$currentValue.value = handleResolveClick(expression)
expression = ''
}
}
function handleOperator({ currentValue, operator }) {
if(document.querySelector('.expresion-equal')){
$expresion.textContent = ''
$expresion.insertAdjacentHTML(
'beforeend',
`<span>${$currentValue.value}</span>
<span>${operator}</span>`
)
$currentValue.value = 0
}else{
$expresion.insertAdjacentHTML(
'beforeend',
`<span>${currentValue}</span>
<span>${operator}</span>`
)
$currentValue.value = 0
}
}
function handleClearClick() {
$expresion.textContent = ''
$currentValue.value = 0
}
function handleRemoveClick() {
if ($currentValue.value !== '0') {
$currentValue.value = $currentValue.value.slice(0, -1)
}
if ($currentValue.value === '') {
$currentValue.value = 0
}
}
function handleResolveClick(equation) {
try {
return eval(equation)
} catch (error) {
return 'SINTAXIS ERROR'
}
}
$currentValue.addEventListener('input', (e) => {
handleChangeInput(e.target.value)
})