-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
218 lines (196 loc) · 5.4 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
let display = document.getElementById("screen");
let buttons = document.getElementsByClassName("button");
Array.prototype.forEach.call(buttons, function (button) {
button.addEventListener("click", function () {
if (
button.textContent != "=" &&
button.textContent != "C" &&
button.textContent != "x" &&
button.textContent != "÷" &&
button.textContent != "√" &&
button.textContent != "x²" &&
button.textContent != "%" &&
button.textContent != "<=" &&
button.textContent != "±" &&
button.textContent != "sin" &&
button.textContent != "cos" &&
button.textContent != "tan" &&
button.textContent != "log" &&
button.textContent != "ln" &&
button.textContent != "x^" &&
button.textContent != "x!" &&
button.textContent != "π" &&
button.textContent != "e" &&
button.textContent != "³√" &&
button.textContent != "n√" &&
button.textContent != "x³"
) {
display.value += button.textContent;
} else if (button.textContent === "=") {
equals();
} else if (button.textContent === "C") {
clear();
} else if (button.textContent === "x") {
multiply();
} else if (button.textContent === "÷") {
divide();
} else if (button.textContent === "±") {
plusMinus();
} else if (button.textContent === "<=") {
backspace();
} else if (button.textContent === "%") {
percent();
} else if (button.textContent === "π") {
pi();
} else if (button.textContent === "x²") {
square();
} else if (button.textContent === "√") {
squareRoot();
} else if (button.textContent === "sin") {
sin();
} else if (button.textContent === "cos") {
cos();
} else if (button.textContent === "tan") {
tan();
} else if (button.textContent === "log") {
log();
} else if (button.textContent === "ln") {
ln();
} else if (button.textContent === "x^") {
exponent();
} else if (button.textContent === "x!") {
factorial();
} else if (button.textContent === "e") {
exp();
} else if (button.textContent === "³√") {
cubeRoot();
} else if (button.textContent === "n√") {
nRoot();
} else if (button.textContent === "x³") {
cube();
}
});
});
function equals() {
if (display.value.indexOf("^") > -1) {
let base = display.value.slice(0, display.value.indexOf("^"));
let exponent = display.value.slice(display.value.indexOf("^") + 1);
display.value = eval("Math.pow(" + base + "," + exponent + ")");
} else if (display.value.indexOf("√") > -1) {
let root = display.value.slice(0, display.value.indexOf("√"));
let baseR = display.value.slice(display.value.indexOf("√") + 1);
display.value = eval("Math.pow(" + baseR + ", 1/" + root + ")");
} else {
try {
display.value = eval(display.value);
} catch (error) {
alert(error);
clear();
}
}
}
function clear() {
display.value = "";
}
function backspace() {
display.value = display.value.substring(0, display.value.length - 1);
}
function multiply() {
display.value += "*";
}
function divide() {
display.value += "/";
}
function plusMinus() {
if (display.value.charAt(0) === "-") {
display.value = display.value.slice(1);
} else {
display.value = "-" + display.value;
}
}
function factorial() {
var number = 1;
if (display.value === 0) {
display.value = "1";
} else if (display.value < 0) {
display.value = "undefined";
} else {
var number = 1;
for (var i = display.value; i > 0; i--) {
number *= i;
}
display.value = number;
}
}
function pi() {
let lastChar = display.value.charAt(display.value.length - 1);
if (
lastChar === "*" ||
lastChar === "/" ||
lastChar === "+" ||
lastChar === "-" ||
lastChar === "^" ||
lastChar === "√"
) {
console.log("si");
display.value += Math.PI;
} else {
console.log("no");
display.value = Math.PI;
}
}
function square() {
display.value = eval(display.value * display.value);
}
function squareRoot() {
display.value = Math.sqrt(display.value);
}
function cube() {
display.value = eval(display.value * display.value * display.value);
}
function cubeRoot() {
display.value = Math.cbrt(display.value);
}
function percent() {
sum = display.value.indexOf("+");
res = display.value.indexOf("-");
if (sum >= 0 && res >= 0) {
alert("No se puede calcular así");
clear();
} else if (sum >= 0 && res < 0) {
num1 = parseFloat(display.value.slice(0, sum));
num2 = parseFloat(display.value.slice(sum + 1, display.value.length));
display.value = num1 + num1 * (num2 / 100);
} else if (res >= 0 && sum < 0) {
num1 = parseFloat(display.value.slice(0, res));
num2 = parseFloat(display.value.slice(res + 1, display.value.length));
display.value = num1 - num1 * (num2 / 100);
} else {
alert("No se puede calcular");
clear();
}
}
function sin() {
display.value = Math.sin(display.value);
}
function cos() {
display.value = Math.cos(display.value);
}
function tan() {
display.value = Math.tan(display.value);
}
function log() {
display.value = Math.log10(display.value);
}
function ln() {
display.value = Math.log(display.value);
}
function exponent() {
display.value += "^";
}
function nRoot() {
display.value += "√";
}
function exp() {
display.value = Math.exp(display.value);
}