-
Notifications
You must be signed in to change notification settings - Fork 0
/
calc.js
114 lines (95 loc) · 4.77 KB
/
calc.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
let display = document.getElementById('display');
/* --------- Calculator --------- */
// function for calculating operations
const calculate = (n1, operator, n2) => {
let first = parseFloat(n1);
let second = parseFloat(n2);
if (operator === 'add') return first + second
if (operator === 'subtract') return first - second
if (operator === 'multiply') return first * second
if (operator === 'divide') return first / second
}
const calculator = document.querySelector('.calculator');
const keys = document.querySelector('.calc_keys');
keys.addEventListener('click', e => {
if (e.target.matches('button')){
const key = e.target; //capturing the element that triggered the event (aka click)
const action = key.dataset.action; //capturing the dataset attribute of this element
const keyContent = key.textContent; //capturing the no. of the key clicked
const displayedNum = display.textContent //capturing the currently displayed no.
const previousKeyType = calculator.dataset.previousKeyType
// key inputs
if (!action) {
if (displayedNum === '0' || previousKeyType === 'operator' || previousKeyType === 'calculate'){
display.textContent = keyContent;
} else {
display.textContent = displayedNum + keyContent;
}
calculator.dataset.previousKeyType = 'number'
}
// concatenating decimal after number input
if (action === 'decimal'){
if (!displayedNum.includes('.')){ // Do nothing if decimal already included
display.textContent = displayedNum + '.'
} else if (previousKeyType === 'operator' || previousKeyType === 'calculate'){
display.textContent = '0.'
}
calculator.dataset.previousKeyType = 'decimal'
}
// depressing op keys on click so user aware of current op
if (action === 'add' || action === 'subtract' || action === 'multiply' || action === 'divide'){
const firstValue = calculator.dataset.firstValue
const operator = calculator.dataset.operator
const secondValue = displayedNum
if (
firstValue &&
operator &&
previousKeyType !== 'operator' &&
previousKeyType !== 'calculate'
) {
const calcValue = calculate(firstValue, operator, secondValue)
display.textContent = calcValue
calculator.dataset.firstValue = calcValue // Update calculated value as firstValue
} else {
calculator.dataset.firstValue = displayedNum // If there are no calculations, set displayedNum as the firstValue
}
key.classList.add('is-depressed')
calculator.dataset.previousKeyType = 'operator'
calculator.dataset.operator = action
}
// remove new class for next no. input w/ forEach loop
Array.from(key.parentNode.children) // 1. HTMLcollection of all key parent's child elements
// 2. String converted toArray for loop to work
.forEach(k => k.classList.remove('.is-depressed'));
if (action === 'calculate') {
let firstValue = calculator.dataset.firstValue
const operator = calculator.dataset.operator
let secondValue = displayedNum
if (firstValue) {
if (previousKeyType === 'calculate') {
firstValue = displayedNum // Make current displayed no. the end calc for continuous calc to work
secondValue = calculator.dataset.modValue // Allows calc to occur if second value exists
}
display.textContent = calculate(firstValue, operator, secondValue) // Prevent calc when op keys not yet clicked
}
calculator.dataset.modValue = secondValue // Custom attribute added to include 2nd value during continuous calc
calculator.dataset.previousKeyType = 'calculate'
}
if (action === 'clear'){
if (key.textContent === 'AC') {
calculator.dataset.firstValue = ''
calculator.dataset.modValue = ''
calculator.dataset.operator = ''
calculator.dataset.previousKeyType = ''
} else {
key.textContent = 'AC' // reverts from CE back to AC
}
display.textContent = 0
calculator.dataset.previousKeyType = 'clear'
}
if (action !== 'clear'){
const clearBtn = calculator.querySelector('[data-action=clear]')
clearBtn.textContent = 'CE' // Hitting any key except clear should change AC to CE
}
}
})