Skip to content

Commit a841cfa

Browse files
author
Saurabh Maurya
committed
added calculation history
1 parent 779ed3d commit a841cfa

File tree

5 files changed

+100
-1
lines changed

5 files changed

+100
-1
lines changed

calc.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ document.getElementById("answer").readOnly = true; //set this attribute in Html
66
let screen = document.getElementById("answer");
77
buttons = document.querySelectorAll("button");
88
let screenValue = "";
9+
let maxItems = 6;
10+
911
for (item of buttons) {
1012
item.addEventListener("click", (e) => {
1113
buttonText = e.target.innerText;
@@ -98,5 +100,11 @@ function checkForBracketMulti() {
98100
return;
99101
} else {
100102
screen.value = eval(screenValue);
103+
let calcHistory = JSON.parse(localStorage.getItem("calcHistory")) || [];
104+
if(calcHistory.length >= maxItems){
105+
calcHistory.shift();
106+
}
107+
calcHistory.push({screenValue, result : screen.value});
108+
localStorage.setItem("calcHistory", JSON.stringify(calcHistory));
101109
}
102110
}

hist.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
let historybutton = document.getElementById('historybutton');
2+
let history = document.getElementById('history');
3+
let bar1 = document.getElementById('bar1');
4+
let bar2 = document.getElementById('bar2');
5+
6+
function showHistory() {
7+
let calcHistory = JSON.parse(localStorage.getItem("calcHistory")) || [];
8+
let len = calcHistory.length;
9+
10+
history.innerHTML = '';
11+
12+
bar1.style.display = 'block';
13+
bar2.style.display = 'block';
14+
if (len === 0) {
15+
let historyItem = document.createElement('div');
16+
historyItem.innerHTML = "There's no history yet.";
17+
historyItem.className = 'historyelement his';
18+
historyItem.style.fontSize = '30px';
19+
history.appendChild(historyItem);
20+
} else {
21+
for (let index = 0; index < len; index++) {
22+
const element = calcHistory[index];
23+
let historyItem = document.createElement('div');
24+
historyItem.className = 'historyelement';
25+
historyItem.innerHTML = `${element.screenValue} = ${element.result}`;
26+
history.appendChild(historyItem);
27+
if (index < len - 1) history.appendChild(document.createElement('hr'));
28+
}
29+
}
30+
history.style.display = 'block';
31+
}
32+
33+
historybutton.addEventListener('click', showHistory);
34+
35+
function hide(){
36+
history.style.display = 'none';
37+
bar1.style.display = 'none';
38+
bar2.style.display = 'none';
39+
}
40+
41+
bar1.addEventListener('click', hide);
42+
bar2.addEventListener('click', hide);

history.png

1023 Bytes
Loading

index.html

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363

6464
<body>
6565
<div class="container">
66-
<h1><a style="text-decoration: none; color: #f1c40f; margin-left: 0.25rem;" href="" onclick="location.reload();">CALCULATOR</a></h1 >
66+
<h1><a style="text-decoration: none; color: #f1c40f; margin-left: 0.25rem;" href="" onclick="location.reload();">CALCULATOR</a><img id="historybutton" src="history.png"></h1 >
6767
<div class="calculator">
6868
<input type="text" name="screen" id="answer" readonly>
6969
<table>
@@ -103,9 +103,14 @@ <h1><a style="text-decoration: none; color: #f1c40f; margin-left: 0.25rem;" href
103103
target="_blank" href="https://harsh98trivedi.github.io">Harsh Trivedi</a></div>
104104
</div>
105105
</div>
106+
<div id="bar1" class = "bars"></div>
107+
<div id="bar2" class = "bars"></div>
108+
<div id="history">
109+
</div>
106110
<div id="turn">
107111
PLEASE TURN YOUR DEVICE
108112
</div>
109113
</body>
114+
<script src="hist.js"></script>
110115
<script src="calc.js"></script>
111116
</html>

style.css

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,50 @@ a:hover{
112112
position: fixed;
113113
}
114114

115+
#historybutton{
116+
width:40px;
117+
height:40px;
118+
border-radius: 20px;
119+
float:right;
120+
margin-right:15px;
121+
cursor: pointer;
122+
}
123+
124+
#history{
125+
position: absolute;
126+
width:90%;
127+
height: 80vh;
128+
top:10vh;
129+
background-color: white;
130+
border: 2px solid black;
131+
border-radius: 10px;
132+
display: none;
133+
/* overflow-y: scroll; */
134+
}
135+
136+
.historyelement{
137+
color: black;
138+
margin: 20px;
139+
font-size: 40px;
140+
}
141+
142+
#bar1, #bar2{
143+
position: absolute;
144+
width:30px;
145+
height: 4px;
146+
background-color: white;
147+
margin-top: 20px;
148+
margin-right: 20px;
149+
margin-left: 93%;
150+
transform: rotate(45deg);
151+
cursor: pointer;
152+
display: none;
153+
}
154+
155+
#bar2{
156+
transform: rotate(135deg);
157+
}
158+
115159
@media (orientation: landscape) and (max-height: 500px) {
116160
#turn {
117161
width: 100vw;

0 commit comments

Comments
 (0)