Skip to content

Commit a2a55e3

Browse files
Merge pull request #15 from srbmaury/master
fixed error in history
2 parents 465c536 + ac1dd28 commit a2a55e3

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

calc.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ 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 maxItems = 6;
1314
for (item of buttons) {
1415
item.addEventListener("click", (e) => {
1516
buttonText = e.target.innerText;
@@ -102,6 +103,12 @@ window.onerror = function () {
102103
window.onBracketMultiplication = function () {
103104
screenValue = addStr(screen.value, screen.value.indexOf("("), "*");
104105
screen.value = eval(screenValue);
106+
let calcHistory = JSON.parse(localStorage.getItem("calcHistory")) || [];
107+
if(calcHistory.length >= maxItems){
108+
calcHistory.shift();
109+
}
110+
calcHistory.push({screenValue, result : screen.value});
111+
localStorage.setItem("calcHistory", JSON.stringify(calcHistory));
105112
};
106113

107114
function addStr(str, index, stringToAdd) {
@@ -120,6 +127,12 @@ function checkForBracketMulti() {
120127
return;
121128
} else {
122129
screen.value = eval(screenValue);
130+
let calcHistory = JSON.parse(localStorage.getItem("calcHistory")) || [];
131+
if(calcHistory.length >= maxItems){
132+
calcHistory.shift();
133+
}
134+
calcHistory.push({screenValue, result : screen.value});
135+
localStorage.setItem("calcHistory", JSON.stringify(calcHistory));
123136
}
124137
flag = 1;
125138
}

hist.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ function showHistory() {
1818
historyItem.style.fontSize = '30px';
1919
history.appendChild(historyItem);
2020
} else {
21-
for (let index = 0; index < len; index++) {
21+
for (let index = len-1; index >=0; index--) {
2222
const element = calcHistory[index];
2323
let historyItem = document.createElement('div');
2424
historyItem.className = 'historyelement';
2525
historyItem.innerHTML = `${element.screenValue} = ${element.result}`;
2626
history.appendChild(historyItem);
27-
if (index < len - 1) history.appendChild(document.createElement('hr'));
27+
if (index > 0) history.appendChild(document.createElement('hr'));
2828
}
2929
}
3030
history.style.display = 'block';

0 commit comments

Comments
 (0)