Skip to content

Commit daf683d

Browse files
authored
Merge pull request #17 from josueJURE/display-history-using-HTML
Display history using html
2 parents ccb9c09 + 4a8ad94 commit daf683d

File tree

3 files changed

+94
-34
lines changed

3 files changed

+94
-34
lines changed

index.css

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
outline: none;
66
font-family: "Open Sans", sans-serif;
77
background-color: aliceblue;
8+
list-style: none;
89
}
910

1011
body {
@@ -17,18 +18,17 @@ body {
1718
}
1819

1920
.parent {
21+
position: relative;
2022
height: 600px;
2123
width: 400px;
2224
border: black 1px solid;
2325
border-radius: 15px;
2426
overflow: hidden;
25-
display: grid;
26-
background-color: rgb(27 22 22 / 39%);
27-
justify-content: center;
28-
27+
2928
}
3029

3130
.sliding-part {
31+
position: absolute;
3232
height: 530px;
3333
width: 400px;
3434
border: black 1px solid;
@@ -41,10 +41,48 @@ body {
4141
padding: 10px;
4242
background-color: rgb(27 22 22 / 39%);
4343
transform: translateX(0px);
44-
transition: transform 2s;
45-
46-
44+
transition: transform 1s;
45+
}
46+
47+
.computation-history-parent {
48+
visibility: hidden;
4749
}
50+
51+
.computation-history-parent.visility {
52+
visibility: visible;
53+
}
54+
55+
.computation-history {
56+
height: 490px;
57+
width: 400px;
58+
display: flex;
59+
flex-direction: column;
60+
padding-top: 10px;
61+
row-gap: 10px;
62+
}
63+
64+
.computation-history-parent > * {
65+
margin-left: 10px;
66+
}
67+
68+
69+
70+
.clear-history-btn {
71+
width: 100px;
72+
margin-top: -10px;
73+
74+
border-radius: 15px;
75+
display: none;
76+
77+
}
78+
79+
.clear-history-btn.display {
80+
display: block;
81+
82+
}
83+
84+
85+
4886
.history-btn,
4987
.child {
5088
border: 1px black solid;
@@ -56,7 +94,7 @@ body {
5694

5795
.sliding-part.slide {
5896
transform: translateX(200px);
59-
transition: transform 2s;
97+
transition: transform 1s;
6098

6199

62100
}
@@ -88,3 +126,5 @@ body {
88126
grid-column-end: 2;
89127
}
90128

129+
130+

index.html

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,15 @@
3333
<div data-value="history" class="history-btn">history</div>
3434
<div data-value="(" data-operator class="child operator">(</div>
3535
<div data-value=")" data-operator class="child operator">)</div>
36-
36+
</div>
37+
38+
<div class="computation-history-parent">
39+
<h3>history</h3>
40+
<ul class="computation-history"></ul>
41+
<button class="clear-history-btn">clear history</button>
3742

3843
</div>
44+
3945

4046

4147

index.js

Lines changed: 39 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
11
const btns = document.querySelectorAll("[data-value]");
2+
const historyElement = document.querySelector(".computation-history");
23
let screen = document.querySelector("[data-screen]");
34
const historyBtn = document.querySelector(".history-btn");
45
const slidingPart = document.querySelector(".sliding-part");
6+
const computationHistoryParent = document.querySelector(".computation-history-parent");
57
const operators = document.querySelectorAll("[data-operator]");
8+
const clearHistoryBtn = document.querySelector(".clear-history-btn");
9+
10+
clearHistoryBtn.addEventListener("click", () => {historyElement.innerHTML = "";
11+
});
612

713
const operatorRegex = /[\/*\-+]/;
814
const ZERO = 0;
9-
const ZERO_DOT = '0.';
15+
const ZERO_DOT = "0.";
16+
const HISTORY_LIMIT = 10;
1017
const history = [];
1118

12-
console.log(slidingPart)
13-
14-
1519
historyBtn.addEventListener("click", () => {
16-
slidingPart.classList.toggle("slide")
17-
console.log("history btn")
18-
})
19-
20-
21-
20+
slidingPart.classList.toggle("slide");
21+
computationHistoryParent.classList.toggle("visility");
22+
});
2223

2324
let data = [];
2425

@@ -47,8 +48,6 @@ btns.forEach((btn) => {
4748
deteLastEntry(buttonValue);
4849

4950
convertToPercentage(buttonValue);
50-
51-
5251
});
5352
});
5453
// forEach ends & functions creations begins
@@ -58,6 +57,10 @@ function convertToPercentage(button) {
5857
}
5958
}
6059

60+
clearHistoryBtn.addEventListener("click", () => {
61+
historyElement.innerHTML = "";
62+
});
63+
6164
function deteLastEntry(button) {
6265
if (button === "DE") {
6366
let newArray = data.slice(ZERO, -1);
@@ -73,7 +76,6 @@ function canUserAddDot(button) {
7376
if (button === ".") {
7477
var dotAllowed = true;
7578
for (var i = data.length - 1; i >= ZERO; i--) {
76-
console.log("data > " + data[i]);
7779
if (data[i] === ".") {
7880
dotAllowed = false;
7981
break;
@@ -106,15 +108,13 @@ function toggleSign(button) {
106108
let currentExpression = data.join("");
107109
let reversedExpression = currentExpression.split("").join("");
108110
let match = reversedExpression.match(/(\d+(\.\d+)?)|(\D+)/); // Match a number or non-digit
109-
// debugger
110111

111112
if (match) {
112113
let start = currentExpression.length - match[ZERO].length;
113114
let end = currentExpression.length;
114115
let currentValue = Number(match[ZERO]);
115116

116117
if (!isNaN(currentValue)) {
117-
// If it's a number, toggle its sign
118118
currentValue = -currentValue;
119119
data = data
120120
.slice(ZERO, start)
@@ -187,24 +187,20 @@ function userClicksOnEqualButton(button) {
187187
const replacedArray = data.map((item) =>
188188
item === "x" ? "*" : item === "÷" ? "/" : item
189189
);
190-
// Check if the expression involves 0/0
191-
// if (areYouDivindingByZero(replacedArray)) {
192-
// screen.innerText = "You cannot divide by zero. Press AC";
193-
// }
194-
195190
if (areYouDividingdZeroByZero(replacedArray)) {
196191
screen.innerText = "0÷0 is an invalid format. Press AC";
197192
} else {
198193
let result = eval(replacedArray.join(""));
194+
let historyEntries = [[...replacedArray, "=", result]]; // Used slice() at firest. But slice() is not sufficient because it only creates a shallow copy of the array, and modifications to the new array will still affect the original array. The spread syntax ([...replacedArray]), which creates a shallow copy as well, is a concise way to create a new array with the same elements as the existing array. While ensuring that modifications to historyEntries do not affect replacedArray, and vice versa.
199195
replacedArray.splice(replacedArray.length, 0, "=", result);
200-
history.push(replacedArray.join(""))
201-
console.log(history);
202-
203196
displayResult(replacedArray, result);
204-
screen.innerText = !Number.isFinite(result) ? "You cannot divide by zero. Press AC" : result;
205-
// divideByZero(screen, result);
197+
screen.innerText = !Number.isFinite(result)
198+
? "You cannot divide by zero. Press AC"
199+
: result;
206200
data = [];
207201
data.push(result);
202+
createHistoryList(historyEntries, historyElement, history);
203+
togglesClearHistoryButton(historyElement, clearHistoryBtn);
208204
}
209205
} catch (e) {
210206
screen.innerText = `${e.name} press AC`;
@@ -238,4 +234,22 @@ function displayResult(array, outcome) {
238234
array = [];
239235
array.push(outcome);
240236
}
237+
238+
function createHistoryList(array, element, history) {
239+
array.forEach((entry) => {
240+
history.push(entry);
241+
element.innerHTML += `<li> ${entry.join(" ")}</li>`;
242+
if (element.childElementCount > HISTORY_LIMIT) {
243+
element.firstElementChild.remove();
244+
}
245+
});
246+
}
247+
clearHistoryBtn.addEventListener("click", () => {
248+
historyElement.innerHTML = "";
249+
togglesClearHistoryButton(historyElement, clearHistoryBtn);
250+
});
251+
252+
function togglesClearHistoryButton(element, btn) {
253+
btn.classList.toggle("display", element.childElementCount > 0);
254+
}
241255
// functions creations ends

0 commit comments

Comments
 (0)