-
Notifications
You must be signed in to change notification settings - Fork 18
/
index.js
176 lines (171 loc) · 5.66 KB
/
index.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
// Add functionalities for all buttons here
// and can also add more functionalities with proper Comments
//global variables
// these variables are used for implementing x^y button
var normal = "#result-str .normal"; // selector
var superscript = "#result-str .superscript"; // selector
var isSuper = false; // superscript
var normalText = "0"; // normal text
var superscriptText = "0"; // superscript text
var timesClicked = 0; // number of times x^y button was clicked
function typed(value, element) {
if (isSuper) element = superscript;
else element = normal;
let n = document.querySelector(element).innerText;
if (n == "0" && n.length == 1)
//to prevent typing 0 in beginning
document.querySelector(element).innerText = value;
else document.querySelector(element).innerText += value;
}
function displayAnswer(ans) {
if (!isNaN(ans)) {
document.getElementById("answer").innerHTML = ans;
} else {
document.getElementById("answer").innerHTML = "Incorrect Expression";
document.querySelector("#answer").style.fontSize = ".6em"; // getElementById also works
document.querySelector("#answer").style.fontWeight = "bold"; // getElementById also works
}
}
function trigo(func) {
let n = document.querySelector(normal).innerHTML;
let n_rad = n * (Math.PI / 180); // convert to radians
let ans = "";
if (func == "sin") {
ans = Math.sin(n_rad);
document.querySelector(normal).innerHTML = `sin(${n})`;
} else if (func == "cos") {
ans = Math.cos(n_rad);
document.querySelector(normal).innerHTML = `cos(${n})`;
} else if (func == "tan") {
ans = Math.tan(n_rad);
document.querySelector(normal).innerHTML = `tan(${n})`;
}
displayAnswer(ans);
}
function logarithm(func) {
let n = document.querySelector(normal).innerHTML;
let ans = "";
if (func == "log") {
ans = Math.log10(n);
document.querySelector(normal).innerHTML = `log(${n})`;
} else if (func == "ln") {
ans = Math.log(n);
document.querySelector(normal).innerHTML = `ln(${n})`;
}
displayAnswer(ans);
}
function pow(func) {
let n = document.querySelector(normal).innerHTML;
let ans = "";
if (func == "sqrt") {
ans = Math.pow(n, 1 / 2);
document.querySelector(normal).innerHTML = `√(${n})`;
} else if (func == "cbrt") {
ans = Math.pow(n, 1 / 3);
document.querySelector(normal).innerHTML = `∛(${n})`;
} else if (func == "squared") {
ans = Math.pow(n, 2);
document.querySelector(normal).innerHTML = `(${n})^2`;
} else if (func == "cubbed") {
ans = Math.pow(n, 3);
document.querySelector(normal).innerHTML = `(${n})^3`;
}
displayAnswer(ans);
}
function reset() {
isSuper = false;
normalText = "0";
superscriptText = "0";
timesClicked = 0;
}
function displaySavedAnswer() {
const answer = localStorage.getItem("answer");
if (answer) {
document.getElementById("savedAnswer").innerHTML = answer;
}
}
function save(ans) {
localStorage.setItem("answer", ans);
}
window.onload = function () {
/* display previous answer at start */
displaySavedAnswer();
/* Clear */
document.getElementById("clear").onclick = function () {
document.querySelector(normal).innerHTML = "0";
document.querySelector(superscript).innerHTML = "";
document.getElementById("answer").innerHTML = "0";
reset();
};
/* del */
document.getElementById("del").onclick = function () {
if (isSuper) element = superscript;
else element = normal;
let s = document.querySelector(element).innerHTML;
let length = s.length;
if (length == 1) document.querySelector(element).innerHTML = 0;
else document.querySelector(element).innerHTML = s.slice(0, length - 1);
};
/* equal */
document.getElementById("equal-btn").onclick = function () {
reset();
ans = eval(document.querySelector(normal).innerHTML);
displayAnswer(ans);
displaySavedAnswer();
save(ans);
};
/* percent */
document.getElementById("percent").onclick = function () {
let ans = document.querySelector(normal).innerHTML / 100;
typed("%", normal);
displayAnswer(ans);
};
document.getElementById("sin").onclick = function () {
trigo("sin");
};
document.getElementById("cos").onclick = function () {
trigo("cos");
};
document.getElementById("tan").onclick = function () {
trigo("tan");
};
document.getElementById("log").onclick = function () {
logarithm("log");
};
document.getElementById("ln").onclick = function () {
logarithm("ln");
};
document.getElementById("sqrt").onclick = function () {
pow("sqrt");
};
document.getElementById("cbrt").onclick = function () {
pow("cbrt");
};
document.getElementById("x-pow-y").onclick = function () {
++timesClicked;
const normalElement = document.querySelector(normal);
const superElement = document.querySelector(superscript);
if (timesClicked == 1) {
superElement.innerHTML = "0";
normalElement.style.border = "1px solid white";
} else if (timesClicked == 2) {
normalText = normalElement.innerHTML;
isSuper = true;
normalElement.style.border = "0px";
superElement.style.border = "1px solid white";
} else if (timesClicked == 3) {
isSuper = false;
superscriptText = superElement.innerHTML;
superElement.style.border = "0px";
ans = Math.pow(normalText, superscriptText);
displayAnswer(ans);
timesClicked = 0;
}
};
document.getElementById("squared").onclick = function () {
pow("squared");
};
document.getElementById("cubbed").onclick = function () {
pow("cubbed");
};
};