forked from harsh98trivedi/Simple-JavaScript-Calculator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalc.js
More file actions
138 lines (132 loc) · 3.68 KB
/
Copy pathcalc.js
File metadata and controls
138 lines (132 loc) · 3.68 KB
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
console.log(
"Javascript Calculator Made by Harsh Trivedi\nhttps://harsh98trivedi.github.io"
);
let flag = 0;
function isNumber(char) {
return /^\d$/.test(char);
}
document.getElementById("answer").readOnly = true; //set this attribute in Html file
let screen = document.getElementById("answer");
buttons = document.querySelectorAll("button");
let screenValue = "";
let maxItems = 6;
for (item of buttons) {
item.addEventListener("click", (e) => {
buttonText = e.target.innerText;
if (buttonText == "X") {
if (flag == 1) {
flag = 0;
}
buttonText = "*";
screenValue += buttonText;
screen.value = screenValue;
} else if (buttonText == "C") {
if (flag == 1) {
flag = 0;
}
screenValue = "";
screen.value = screenValue;
} else if (buttonText == "=") {
checkForBracketMulti(); // automatically evaluates if no brackets
//
} else if (isNumber(buttonText)) {
if (flag == 1) {
screenValue = buttonText;
flag = 0;
} else {
screenValue += buttonText;
}
screen.value = screenValue;
console.log("im an operator");
} else {
if (flag == 1) {
flag = 0;
}
screenValue = screen.value + buttonText;
screen.value = screenValue;
}
});
}
document.addEventListener("keydown", function (event) {
if (event.shiftKey == 57) {
event.key = "(";
} else if (event.shiftKey == 48) {
event.key = ")";
} else if (event.shiftKey == 53) {
event.key = "%";
}
if (event.keyCode == 88) {
screenValue += "*";
screen.value = screenValue;
}
if (
event.key <= 9 ||
event.key == "+" ||
event.key == "-" ||
event.key == "*" ||
event.key == "." ||
event.key == "/" ||
event.key == "%" ||
event.key == "(" ||
event.key == ")"
) {
screenValue += event.key;
screen.value = screenValue;
}
if (event.keyCode == 13 || event.keyCode == 187) {
checkForBracketMulti(); // automatically evaluates if no brackets
} else if (event.keyCode == 46) {
screenValue = "";
screen.value = screenValue;
console.clear();
} else if (event.keyCode == 8) {
screenValue = screenValue.slice(0, -1);
screen.value = screenValue;
} else if (event.keyCode == 67) {
screenValue = "";
screen.value = screenValue;
console.clear();
} else if (event.keyCode == 82) {
location.reload();
}
});
window.onerror = function () {
alert("PLEASE INPUT VALID EXPRESSION");
screenValue = "";
screen.value = screenValue;
console.clear();
};
window.onBracketMultiplication = function () {
screenValue = addStr(screen.value, screen.value.indexOf("("), "*");
screen.value = eval(screenValue);
let calcHistory = JSON.parse(localStorage.getItem("calcHistory")) || [];
if(calcHistory.length >= maxItems){
calcHistory.shift();
}
calcHistory.push({screenValue, result : screen.value});
localStorage.setItem("calcHistory", JSON.stringify(calcHistory));
};
function addStr(str, index, stringToAdd) {
return (
str.substring(0, index) + stringToAdd + str.substring(index, str.length)
);
}
function checkForBracketMulti() {
// Check if there's a number directly infront of a bracket
if (
screen.value.includes("(") &&
!isNaN(screen.value.charAt(screen.value.indexOf("(") - 1))
) {
window.onBracketMultiplication();
return;
} else {
screen.value = eval(screenValue);
let calcHistory = JSON.parse(localStorage.getItem("calcHistory")) || [];
if(calcHistory.length >= maxItems){
calcHistory.shift();
}
calcHistory.push({screenValue, result : screen.value});
localStorage.setItem("calcHistory", JSON.stringify(calcHistory));
}
flag = 1;
}