-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalc.js
222 lines (211 loc) · 5.63 KB
/
calc.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
219
220
221
222
let first = "";
let opp = "";
let opp_t = "";
let first_opp = false;
let second = "";
let third = "";
let second_opp = false;
let temp = document.getElementById("calcDisplay");
let tempresult = "";
let result = document.getElementById("result");
let posneg = document.getElementById("posneg");
let saved_answers = [];
let clearb = document.getElementById("clearb");
const paramsU = window.location.search;
let searchParams = new URLSearchParams(paramsU);
let link;
let b_One = document.querySelectorAll("one");
let b_Two = document.querySelectorAll("two");
let b_Three = document.querySelectorAll("three");
let b_Four = document.querySelectorAll("four");
let b_Five = document.querySelectorAll("five");
let b_Six = document.querySelectorAll("six");
let b_Seven = document.querySelectorAll("seven");
let b_Eight = document.querySelectorAll("eight");
let b_Nine = document.querySelectorAll("nine");
let b_Zero = document.querySelectorAll("zero");
let num_buttons = document.getElementsByClassName("num");
function getUserDetails() {
let email = document.getElementById("emailText").innerText;
let name = document.getElementById("nameText").innerText;
document.getElementById("emailText").innerText =
email + " " + sessionStorage.email;
document.getElementById("nameText").innerText =
name + "" + sessionStorage.name;
}
for (const n_button of num_buttons) {
let el = n_button;
el.addEventListener("click", (event) => {
if (first_opp == false) {
first = first + el.innerHTML;
tempresult = first;
}
if (first_opp == true && second_opp == false)
second = second + el.innerHTML;
if (second != "") {
if (opp == "+") tempresult = parseInt(first) + parseInt(second);
if (opp == "-") tempresult = parseInt(first) - parseInt(second);
if (opp == "x") tempresult = mult(first, second);
if (opp == "*") tempresult = expo(first, second);
if (opp == "/") tempresult = divsn(first, second);
if (opp == "%") tempresult = percnt(first, second);
temp.innerText = first + " " + opp + " " + second + " = " + tempresult;
}
if (second_opp == true) {
third = third + el.innerHTML;
if (opp_t == "+") tempresult = parseInt(tempresult) + parseInt(third);
if (opp_t == "-") tempresult = parseInt(tempresult) - parseInt(third);
if (opp == "*") tempresult = expo(first, second);
if (opp == "/") tempresult = divsn(first, second);
if (opp == "x") tempresult = mult(first, second);
if (opp == "%") tempresult = percnt(first, second);
temp.innerText =
first +
" " +
opp +
" " +
second +
" " +
opp_t +
" " +
third +
" = " +
tempresult;
}
console.log(tempresult);
temp.innerText =
first +
" " +
opp +
" " +
second +
" " +
opp_t +
" " +
third +
" = " +
tempresult;
});
}
let op_buttons = document.getElementsByClassName("operator");
for (const op_button of op_buttons) {
let el = op_button;
el.addEventListener("click", (event) => {
if (first_opp == false) {
first_opp = true;
opp = el.innerHTML;
temp.innerText = first + " " + opp + " " + second + " = " + tempresult;
if (opp == "%") tempresult = percnt(first);
} else {
second_opp = true;
opp_t = el.innerHTML;
temp.innerText =
first +
" " +
opp +
" " +
second +
" " +
opp_t +
" " +
third +
" = " +
tempresult;
}
});
}
posneg.addEventListener("click", (event) => {
if (first_opp == false) first = parseInt(first) * -1;
else second = parseInt(second) * -1;
temp.innerText = first + " " + opp + " " + second + " = ";
});
result.addEventListener("click", (event) => {
if (second == "" && opp == "%") {
tempresult = percnt(first, 1);
}
temp.innerText = tempresult;
saved_answers.push({
f_num: first,
s_num: second,
sopp: opp,
resu: tempresult,
});
sessionStorage.setItem("answers", JSON.stringify(saved_answers));
first = "";
first_opp = false;
second_opp = false;
second = "";
third = "";
});
clearb.addEventListener("click", (event) => {
first = "";
first_opp = false;
second_opp = false;
second = "";
third = "";
tempresult = "";
temp.innerText = "";
});
function expo(p1, p2) {
let re = parseInt(p1);
for (let i = 1; i < p2; i++) {
re = mult(re, p1);
}
return re;
}
function mult(p1, p2) {
let re = parseFloat(p1);
console.log(re);
let a = 0;
for (let i = 1; i <= p2; i++) {
a = a + re;
}
return a;
}
function divsn(p1, p2) {
let n1 = parseInt(p1);
let n2 = parseInt(p2);
let n = 0;
while (n1 >= n2) {
n++;
n1 = n1 - n2;
}
return n;
}
function percnt(p1, p2) {
let n = ("" + p1).split("");
let p = n[n.length - 1];
let t = n[n.length - 2];
let full_num = 0;
if (t == undefined) {
t = 0;
full_num = "0." + t + p;
} else {
n.pop();
n.pop();
for (let i = 0; i < n.length; i++) {
full_num = full_num + n[i];
}
full_num = full_num + "." + t + p;
}
let lnum = mult(full_num, p2);
return lnum;
}
if(paramsU.indexOf('linked')!=-1)
link=true;
if (link==true)
{
first=searchParams.get('first');
temp.innerText =
first +
" " +
opp +
" " +
second +
" " +
opp_t +
" " +
third +
" = " +
tempresult;
}