-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscripts.js
116 lines (106 loc) · 3.25 KB
/
scripts.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
const numberButtons = document.querySelectorAll('[data-number]')
const operatorButtons = document.querySelectorAll('[data-operator]')
const equalsButton = document.querySelector('[data-equals]')
const clearButton = document.querySelector('[data-clear]')
const display = document.querySelector('[data-display]');
// Global variables
let operator = '';
let previousOperand = '';
let currentOperand = '';
let justCalculated = false;
const clear = function() {
previousOperand = '';
currentOperand = '';
operator = '';
justCalculated = false;
display.textContent = 0;
}
const appendNumber = function (number) {
if (display.textContent == '0' || justCalculated) {
justCalculated = false;
previousOperand = currentOperand
currentOperand = number;
} else if (display.textContent !== '0') {
if (currentOperand.length == 10) {
alert('Stop pushing my buttons. This number is too big. :)');
} else {
currentOperand += number;
}
}
}
const updateDisplay = function () {
if (currentOperand !== '') {
display.textContent = currentOperand;
}
}
const chooseOperation = function (operation) {
if (currentOperand !== '' && previousOperand == '') {
operator = operation;
previousOperand = currentOperand;
currentOperand = '';
justCalculated = false;
} else if (currentOperand == '' && previousOperand !== '' && operation !== operator) {
operator = operation
justCalculated = false;
} else if (previousOperand !== '' && currentOperand !== '') {
operate(previousOperand, currentOperand, operator, operation);
updateDisplay();
}
}
const operate = function(firstOperand, secondOperand, operation, nextOperation) {
let computation;
if (isNaN(firstOperand) || isNaN(secondOperand)) {
return;
} else if (firstOperand == '') {
return;
} else if (secondOperand == '') {
secondOperand = firstOperand;
}
switch (operation) {
case '+':
computation = +firstOperand + +secondOperand;
break;
case '-':
computation = +firstOperand - +secondOperand;
break;
case '/':
if (secondOperand == '0') {
alert('Divde by zero error. Better luck next time ;)');
return;
} else {
computation = +firstOperand / +secondOperand;
}
break;
case 'x':
computation = +firstOperand * +secondOperand;
break;
default:
return
}
if (nextOperation == null) {
operator = '';
} else {
operator = nextOperation
}
currentOperand = computation;
previousOperand = ''
justCalculated = true;
}
numberButtons.forEach(button => {
button.addEventListener('click', () => {
appendNumber(button.textContent);
updateDisplay();
})
})
clearButton.addEventListener('click', () => {
clear();
})
operatorButtons.forEach(button => {
button.addEventListener('click', () => {
chooseOperation(button.textContent);
})
})
equalsButton.addEventListener('click', button => {
operate(previousOperand, currentOperand, operator);
updateDisplay();
})