-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscrpit.js
101 lines (87 loc) · 2.79 KB
/
scrpit.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
const USER_REGISTER = document.querySelector('.register')
const USER_OPERATION = document.querySelector('.operation')
const BUTTONS = document.querySelectorAll('.grid button')
const operation = []
BUTTONS.forEach((button) => button.addEventListener('click', ManageOperations))
function ManageOperations(e) {
const button = e.target
if (button.classList.contains('number')) {
populateVisorOperation(button)
} else if (button.classList.contains('operator')) {
addOperator(button)
} else if (button.classList.contains('clear')) {
clearNumbers()
} else if (button.classList.contains('equals')) {
ShowResult()
}
}
function populateVisorOperation(button) {
if (USER_OPERATION.textContent.length < 10) {
USER_OPERATION.textContent += button.value
}
}
function addOperator(button) {
if (USER_OPERATION.textContent.length >= 1) {
// if the operations is not chained the lenght will be 0
if (operation.length === 0) {
operation.push(USER_OPERATION.textContent, button.value)
USER_REGISTER.textContent = operation.join(' ')
USER_OPERATION.textContent = ''
// if the operation is chained the lenght will be 1
} else if (operation.length === 1) {
operation.push(button.value)
USER_REGISTER.textContent = operation.join(' ')
USER_OPERATION.textContent = ''
}
}
}
function clearNumbers() {
USER_OPERATION.textContent = ''
USER_REGISTER.textContent = ''
operation.splice(0)
}
function ShowResult() {
/* The lenght of the operation is 2 when its ready to be executed because the
last value is evalueted only when there is text and the button '=' is
clicked */
if (operation.length === 2 && USER_OPERATION.textContent.length >= 1) {
operation.push(USER_OPERATION.textContent)
USER_REGISTER.textContent = operation.join(' ') + ' ='
const result = String(operate())
USER_OPERATION.textContent =
result.length > 9 ? result.slice(0, 9) + '...' : result
if (result === 'BOOM') {
setTimeout(function() {
clearNumbers()
}, 1000)
} else {
// The result is set as the first operator, thus setting the lenght to be
// 1 and recogning values as a chained operation
operation.splice(0, 3, result.slice(0, 9))
}
}
}
function operate() {
const [n1, operator, n2] = operation.map((value) =>
isNaN(+value) ? value : +value
)
if (operator === '+') return add(n1, n2)
else if (operator === '-') return subtract(n1, n2)
else if (operator === '*') return multiply(n1, n2)
else if (operator === '/') return divide(n1, n2)
}
function add(n1, n2) {
return n1 + n2
}
function subtract(n1, n2) {
return n1 - n2
}
function multiply(n1, n2) {
return n1 * n2
}
function divide(n1, n2) {
if (n1 / n2 === Infinity) {
return 'BOOM'
}
return Math.round(n1 / n2)
}