-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
143 lines (115 loc) · 4.03 KB
/
script.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
let a = "";
let b = "";
let answer = "";
let oppPressed = false;
let operator = "";
const numbtns = document.querySelectorAll('.num-btn');
const oppbtns = document.querySelectorAll('.opp-btn');
const equalbtn = document.querySelector('.equal-btn');
const clear = document.querySelector('.clear-btn');
const output = document.querySelector('.result');
const input = document.querySelector('.input');
const dlt = document.querySelector('.delete-btn');
equalbtn.addEventListener('click', function() {
output.textContent = operate(a, operator, b);
answer = operate(a, operator, b);
if (answer != "" || answer === 0) {
a = answer;
input.textContent = a;
oppPressed = false;
operator = "";
b = "";
}
});
clear.addEventListener('click', function() {
if (a != "" || b != "" || operator != "" || oppPressed == true || answer != "") {
a = "";
b = "";
operator = "";
oppPressed = false;
answer = "";
input.textContent = "";
output.textContent = "";
}
});
dlt.addEventListener('click', function() {
console.log('test')
if (a != "" && operator == "" && oppPressed == false && b == "" && answer == "") {
input.textContent = input.textContent.toString().slice(0,-1);
a = a.toString().slice(0,-1);
a = answer.toString().slice(0,-1);
}
if (a != "" && operator != "" && oppPressed == true && b == "" && answer == "") {
input.textContent = input.textContent.toString().slice(0,-1);
oppPressed = false;
operator = operator.toString().slice(0,-1);
}
if (a != "" && operator != "" && oppPressed == true && b != "" && answer == "") {
input.textContent = input.textContent.toString().slice(0,-1);
b = b.toString().slice(0,-1);
}
if (a != "" && operator != "" && oppPressed == true && b != "" && answer == "") {
input.textContent = input.textContent.toString().slice(0,-1);
b = b.toString().slice(0,-1);
}
});
numbtns.forEach(function Operand(element) {
element.addEventListener('click', () => {
if(!oppPressed) {
a = a + element.textContent;
input.textContent = a;
} else {
b = b + element.textContent;
input.textContent += element.textContent;
}
});
});
oppbtns.forEach(function (element) {
element.addEventListener('click', () => {
if(!oppPressed && a != "") {
operator = operator += element.textContent;
oppPressed = true;
input.textContent += operator;
}
})
});
//functions for different opereations.
const addition = function() {
const num = [...arguments];
return num.reduce((total, sum) => total + sum , 0);
}
const subtraction = function() {
const num = [...arguments];
return num.reduce((total, remaining) => total - remaining);
}
const multiplication = function() {
const num = [...arguments];
return num.reduce((total, multiple) => total * multiple, 1);
}
const division = function() {
const num = [...arguments];
// round answers with long decimals so that they don’t overflow the screen.
return num.reduce((dividend, divisor) => Math.round(dividend / divisor * 1000)/ 1000);
}
//function that'll excute the operation according to the users operator of choice.
function operate(a, operator, b) {
let num1 = Number(a);
let num2 = Number(b);
switch (operator) {
case '+':
return addition(num1, num2);
case '-':
return subtraction(num1, num2);
case 'x':
return multiplication(num1, num2);
case '÷':
if (b == 0) return answer = "Try again buddy";
else return division(num1, num2);
default:
if (a != "") {
return a * 1;
} else {
return ""
}
}
}