Skip to content

Commit 6f94cee

Browse files
committed
1 parent 609d135 commit 6f94cee

File tree

2 files changed

+38
-32
lines changed

2 files changed

+38
-32
lines changed

calc.js

Lines changed: 37 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,18 @@ document.getElementById("answer").readOnly = true; //set this attribute in Html
1010
let screen = document.getElementById("answer");
1111
buttons = document.querySelectorAll("button");
1212
let screenValue = "";
13+
let lastScreenValue = "";
1314
let maxItems = 6;
15+
let isSign = true;
1416
for (item of buttons) {
1517
item.addEventListener("click", (e) => {
1618
buttonText = e.target.innerText;
17-
if (buttonText == "X") {
19+
if (buttonText == "X" && !isSign) {
1820
if (flag == 1) {
1921
flag = 0;
2022
}
2123
buttonText = "*";
24+
isSign = true;
2225
screenValue += buttonText;
2326
screen.value = screenValue;
2427
} else if (buttonText == "C") {
@@ -27,6 +30,7 @@ for (item of buttons) {
2730
}
2831
screenValue = "";
2932
screen.value = screenValue;
33+
isSign = true;
3034
} else if (buttonText == "=") {
3135
checkForBracketMulti(); // automatically evaluates if no brackets
3236
//
@@ -38,42 +42,39 @@ for (item of buttons) {
3842
screenValue += buttonText;
3943
}
4044
screen.value = screenValue;
41-
console.log("im an operator");
45+
isSign = false;
4246
} else {
4347
if (flag == 1) {
4448
flag = 0;
4549
}
46-
screenValue = screen.value + buttonText;
47-
screen.value = screenValue;
50+
if (!isSign){
51+
screenValue = screen.value + buttonText;
52+
screen.value = screenValue;
53+
isSign = true;
54+
}
4855
}
4956
});
5057
}
5158

5259
document.addEventListener("keydown", function (event) {
53-
if (event.shiftKey == 57) {
54-
event.key = "(";
55-
} else if (event.shiftKey == 48) {
56-
event.key = ")";
57-
} else if (event.shiftKey == 53) {
58-
event.key = "%";
59-
}
60-
if (event.keyCode == 88) {
61-
screenValue += "*";
62-
screen.value = screenValue;
63-
}
6460
if (
65-
event.key <= 9 ||
66-
event.key == "+" ||
67-
event.key == "-" ||
68-
event.key == "*" ||
69-
event.key == "." ||
70-
event.key == "/" ||
71-
event.key == "%" ||
72-
event.key == "(" ||
73-
event.key == ")"
61+
event.key <= 9
7462
) {
7563
screenValue += event.key;
7664
screen.value = screenValue;
65+
isSign = false;
66+
}
67+
if(!isSign && (event.key == "+" ||
68+
event.key == "-" ||
69+
event.key == "*" ||
70+
event.key == "." ||
71+
event.key == "/" ||
72+
event.key == "%" ||
73+
event.key == "(" ||
74+
event.key == ")")){
75+
screenValue += event.key;
76+
screen.value = screenValue;
77+
isSign = true;
7778
}
7879
if (event.key == "Enter" || event.key == "=") {
7980
event.preventDefault();
@@ -120,21 +121,26 @@ function addStr(str, index, stringToAdd) {
120121

121122
function checkForBracketMulti() {
122123
// Check if there's a number directly infront of a bracket
124+
isSign = false;
123125
if (
124126
screen.value.includes("(") &&
125127
!isNaN(screen.value.charAt(screen.value.indexOf("(") - 1))
126128
) {
127129
window.onBracketMultiplication();
128130
return;
129131
} else {
130-
screen.value = eval(screenValue);
131-
screenValue = screen.value;
132-
let calcHistory = JSON.parse(localStorage.getItem("calcHistory")) || [];
133-
if (calcHistory.length >= maxItems) {
134-
calcHistory.shift();
132+
if(eval(screenValue) !== undefined)
133+
{
134+
screen.value = eval(screenValue);
135+
lastScreenValue = screenValue;
136+
screenValue = screen.value;
137+
let calcHistory = JSON.parse(localStorage.getItem("calcHistory")) || [];
138+
if (calcHistory.length >= maxItems) {
139+
calcHistory.shift();
140+
}
141+
calcHistory.push({ lastScreenValue, result: screen.value });
142+
localStorage.setItem("calcHistory", JSON.stringify(calcHistory));
135143
}
136-
calcHistory.push({ screenValue, result: screen.value });
137-
localStorage.setItem("calcHistory", JSON.stringify(calcHistory));
138144
}
139145
flag = 1;
140146
}

hist.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ function showHistory() {
2222
const element = calcHistory[index];
2323
let historyItem = document.createElement('div');
2424
historyItem.className = 'historyelement';
25-
historyItem.innerHTML = `${element.screenValue} = ${element.result}`;
25+
historyItem.innerHTML = `${element.lastScreenValue} = ${element.result}`;
2626
history.appendChild(historyItem);
2727
if (index > 0) history.appendChild(document.createElement('hr'));
2828
}

0 commit comments

Comments
 (0)