-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
72 lines (67 loc) · 2.23 KB
/
app.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
let string = "";
let flag = 1;
function evaluateExpression() {
try {
const result = eval(string);
const roundedResult = Number(result.toFixed(6));
document.querySelector(".inp").value = roundedResult;
string = String(result);
} catch (error) {
document.querySelector(".inp").value = "Syntax Error";
string = "";
setTimeout(function() {
document.querySelector(".inp").value = "";
}, 800);
}
}
$(document).ready(function() {
$(".btn").click(function() {
Calculation(this.innerHTML);
});
$(document).keypress(function(event) {
if ((event.key >= 0 && event.key <= 9) || event.key === "=" || event.key === "+" || event.key === "-" || event.key === "x" || event.key === "/" || event.key === "%" || event.key === ".") {
Calculation(event.key);
}
});
});
function Calculation(key) {
if (string.length === 0) {
string = "";
}
if (key === "=") {
evaluateExpression();
} else if (key === "AC") {
string = "";
document.querySelector(".inp").value = string;
} else if (key === "DEL") {
let len = string.length - 1;
string = string.slice(0, len);
document.querySelector(".inp").value = string;
} else {
if (key.toLowerCase() === "x") {
string = string + "*";
} else {
string = string + key;
}
console.log(string);
document.querySelector(".inp").value = string;
}
}
function Theme() {
if (flag == 0) {
$(".calculator").css("background-color", "#FEFEFE");
$(".container").css("background-color", "#F5F5F5");
$(".btn").removeClass("pressed2").addClass("pressed");
$(".inp").css("color", "black");
$(".fa").css("color", "black");
flag = 1;
} else {
$(".calculator").css("background-color", "#36474F");
$(".container").css("background-color", "#18212A");
$(".btn").removeClass("pressed").addClass("pressed2");
$(".inp").css("color", "white");
$(".right").css("color", "black");
$(".fa").css("color", "#fff");
flag = 0;
}
}