-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
146 lines (135 loc) · 4 KB
/
main.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
document.addEventListener("DOMContentLoaded", number);
const operator = ["-", "+", "x", "÷", "%", "^","."];
window.onload = (event) => {
const textarea = document.getElementById("textarea");
document.getElementById("ROD").addEventListener("click", clicked);
let operators = ["plus", "minus", "multiply", "divide"];
operators.forEach((value, index) => {
document
.getElementById(value)
.addEventListener("click", () => addOperator(value));
});
document.getElementById("CE").addEventListener("click", () => {
textarea.value = textarea.value.slice(0, textarea.value.length - 1);
textarea.focus();
});
document.getElementById("ps").addEventListener("click", () => {
textarea.value += "(";
textarea.focus();
});
document.getElementById("pe").addEventListener("click", () => {
textarea.value += ")";
textarea.focus();
});
document.getElementById("factorial").addEventListener("click", () => {
textarea.value += "!";
textarea.focus();
});
document.getElementById("mod").addEventListener("click", () => {
textarea.value += "%";
textarea.focus();
});
document.getElementById("sin").addEventListener("click", () => {
textarea.value += "sin(";
textarea.focus();
});
document.getElementById("cos").addEventListener("click", () => {
textarea.value += "cos(";
textarea.focus();
});
document.getElementById("tan").addEventListener("click", () => {
textarea.value += "tan(";
textarea.focus();
});
document.getElementById("Nlog").addEventListener("click", () => {
textarea.value += "ln(";
textarea.focus();
});
document.getElementById("log").addEventListener("click", () => {
textarea.value += "log(";
textarea.focus();
});
document.getElementById("exponent").addEventListener("click", () => {
textarea.value += "^";
textarea.focus();
});
document.getElementById("sqrt").addEventListener("click", () => {
textarea.value += "√";
textarea.focus();
});
document.getElementById("eularNo").addEventListener("click", () => {
if (
operator.includes(textarea.value.charAt(textarea.value.length - 1)) ||
textarea.value.length == 0
) {
textarea.value += "e";
} else {
textarea.value += "xe";
}
textarea.focus();
});
document.getElementById("pi").addEventListener("click", () => {
if (
operator.includes(textarea.value.charAt(textarea.value.length - 1)) ||
textarea.value.length == 0
) {
textarea.value += "π";
} else {
textarea.value += "xπ";
}
textarea.focus();
});
textarea.focus();
};
let count = 0;
function clicked() {
const x = document.getElementById("ROD");
if (count == 0) {
count = 1;
document.getElementById("ROD").innerHTML = "Red | <strong>Deg</strong>";
} else {
count = 0;
document.getElementById("ROD").innerHTML = "<strong>Red</strong> | Deg";
}
}
function number() {
for (let index = 0; index < 10; index++) {
const btn = document.getElementById(index.toString());
btn.onclick = () => btnClick(index);
}
document.getElementById("dot").onclick = () => decimal();
function btnClick(id) {
const textarea = document.getElementById("textarea");
textarea.value += id.toString();
textarea.focus();
}
function decimal() {
const textarea = document.getElementById("textarea");
if (textarea.value.toString().charAt(textarea.value.length - 1) != ".") {
textarea.value += ".";
textarea.focus();
}
}
}
function addOperator(opration) {
const textarea = document.getElementById("textarea");
const len = textarea.value.toString().length;
console.log(len);
if (!operator.includes(textarea.value.toString().charAt(len - 1))) {
switch (opration) {
case "plus":
textarea.value += "+";
break;
case "minus":
textarea.value += "-";
break;
case "multiply":
textarea.value += "x";
break;
case "divide":
textarea.value += "÷";
break;
}
}
textarea.focus();
}