forked from DhanushNehru/calculator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
158 lines (137 loc) · 4.3 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
var display = document.getElementById("screen");
var buttons = document.getElementsByClassName("button");
Array.prototype.forEach.call(buttons, function (button) {
button.addEventListener("click", function () {
const trimmedButtonValue = button.textContent.trim();
console.log(" Button text content", trimmedButtonValue);
if (
trimmedButtonValue != "=" &&
trimmedButtonValue != "C" &&
trimmedButtonValue != "x" &&
trimmedButtonValue != "\u00F7" &&
trimmedButtonValue != "\u221A" &&
trimmedButtonValue != "x 2" &&
trimmedButtonValue != "x ²" &&
trimmedButtonValue != "%" &&
trimmedButtonValue != "x^" &&
trimmedButtonValue != "x !" &&
trimmedButtonValue != "e ˣ" &&
trimmedButtonValue != "e x"
) {
display.value += trimmedButtonValue;
} else if (trimmedButtonValue === "=") {
equals();
} else if (trimmedButtonValue === "C") {
clear();
} else if (trimmedButtonValue === "x") {
multiply();
} else if (trimmedButtonValue === "\u00F7") {
divide();
} else if (trimmedButtonValue === "<=") {
backspace();
} else if (trimmedButtonValue === "%") {
percent();
} else if (trimmedButtonValue === "x 2" || trimmedButtonValue === "x ²") {
square();
} else if (trimmedButtonValue === "\u221A") {
squareRoot();
} else if (trimmedButtonValue === "x^") {
exponent();
} else if (trimmedButtonValue === "x !") {
factorial();
} else if (trimmedButtonValue === "(") {
right_bracket();
} else if (trimmedButtonValue === ")") {
left_bracket();
} else if (trimmedButtonValue === "e x" || trimmedButtonValue === "e ˣ") {
exponential();
}
});
});
// Adding key event listener
document.addEventListener("keydown", (e) => {
// Check if the currently pressed key is number
if(/^[0-9]/.test(parseInt(e.key))) display.value += parseInt(e.key);
// Check if the currently pressed key is an operator
if(/^[+\-\*\/\=\(\)\%]*$/.test(e.key)) display.value += e.key;
// Check if the currently pressed key is Backspace, then remove last element
if(e.key === 'Backspace') display.value = display.value.slice(0, -1)
// Check if the currently pressed key is Enter, calculate the value
if(e.key === 'Enter') equals();
// Check if the currently pressed key is 'c', then clear the value
if(e.key === 'c') clear()
});
function right_bracket() {
display.value += "(";
}
function left_bracket() {
display.value += ")";
}
function syntaxError() {
if (
eval(display.value) == SyntaxError ||
eval(display.value) == ReferenceError ||
eval(display.value) == TypeError
) {
display.value == "Syntax Error";
}
}
function equals() {
if (display.value.includes("^")) {
console.log(" Equals1 ", display.value);
display.value = display.value.replaceAll("^", "**");
console.log(" Equals2 ", display.value);
display.value = eval(display.value);
}
// if ((display.value).indexOf("^") > -1) {
// var base = (display.value).slice(0, (display.value).indexOf("^"));
// var exponent = (display.value).slice((display.value).indexOf("^") + 1);
// display.value = eval("Math.pow(" + base + "," + exponent + ")");
// }
else {
display.value = eval(display.value);
//checkLength()
syntaxError();
}
}
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 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 square() {
display.value = eval(display.value * display.value);
}
function squareRoot() {
display.value = Math.sqrt(display.value);
}
function percent() {
display.value = display.value / 100;
}
function exponent() {
display.value += "^";
}
function exponential() {
display.value = eval(Math.exp(display.value));
}