-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
137 lines (131 loc) · 4.12 KB
/
app.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
const operations = (() => {
const add = (x, y) => {
return x + y;
}
const subtract = (x, y) => {
return x - y;
}
const multiply = (x, y) => {
return x * y;
}
const divide = (x, y) => {
return x / y;
}
const operate = (operator, x, y) => {
if (operator === '+') {
return Number(x) + Number(y);
}
else if (operator === '-') {
return Number(x) - Number(y);
}
else if (operator === '*') {
return Number(x) * Number(y);
}
else if (operator === '/') {
if (Number(y) === 0) {
setTimeout(function(){clear()},2*1000);
return 'Hey! You can\'t divide by 0!';
}
else {
return Number(x) / Number(y);
}
}
}
return {
add,
subtract,
multiply,
divide,
operate
}
})();
const numberButtons = document.getElementById('numberButtons').children;
const display = document.getElementById('display');
const operators = ['+', '-', '/', '*'];
let firstNumber = '';
let secondNumber = '';
let operatorClicked = false;
let operation;
//add function to numbers
for (let i = 0; i < numberButtons.length; i++) {
numberButtons[i].onclick = function(){clickNumber(this)};
}
function clickNumber (e) {
if (display.textContent.length < 20) {
display.textContent += e.textContent;
if (!operatorClicked) {
firstNumber += e.textContent;
}
else {
secondNumber += e.textContent;
}
}
}
const operatorButtons = document.getElementById('operatorButtons').children;
//add function to operators
for (let i = 0; i < operatorButtons.length -1; i++) { // -1 so it excludes the '='
operatorButtons[i].onclick = function(){clickOperator(this)};
}
function clickOperator (e) {
if (display.textContent.length < 20) {
if (operatorClicked) {
calculate();
}
display.textContent += e.textContent;
operation = e.textContent;
operatorClicked = true;
}
}
const equalsSign = document.getElementById('operate');
equalsSign.onclick = function(){calculate()};
function calculate () {
display.textContent = operations.operate(operation, firstNumber, secondNumber);
//reset everything
firstNumber = display.textContent;
secondNumber = '';
operatorClicked = false;
}
//clears and resets everything
const clearButton = document.getElementById('clear');
clearButton.onclick = function(){clear()};
function clear () {
display.textContent = '';
firstNumber = '';
secondNumber = '';
operatorClicked = false;
}
//deletes last number / operator input
const deleteButton = document.getElementById('del');
deleteButton.onclick = function(){backspace()};
function backspace () {
for (let i = 0; i < operators.length; i++) {
if (display.textContent[display.textContent.length-1] === operators[i]) {
operatorClicked = false;
operation = 0;
}
}
if (!operatorClicked && !(Number.isNaN(display.textContent[display.textContent.length - 1] / 1))) {
firstNumber = firstNumber.slice(0, -1);
}
else if (operatorClicked && !(Number.isNaN(display.textContent[display.textContent.length - 1] / 1))) {
secondNumber = secondNumber.slice(0, -1);
}
display.textContent = display.textContent.slice(0, -1);
console.log(firstNumber);
console.log(secondNumber);
}
//decimal separator '.'
const decimalButton = document.getElementById('decimalPoint');
decimalButton.onclick = function(){decimal()};
function decimal () {
if (display.textContent.length < 20) {
if (!operatorClicked && ( firstNumber.indexOf('.') === -1 ) ) {
display.textContent += '.';
firstNumber += '.';
}
else if (operatorClicked && ( secondNumber.indexOf('.') === -1 ) ) {
display.textContent += '.';
secondNumber += '.';
}
}
}