<!DOCTYPE html>
<html>
<head>
<title>Parcel Sandbox</title>
<meta charset="UTF-8" />
<link rel="stylesheet" href="./src/styles.css" />
</head>
<body>
<div id="app" class="app__container">
<h1>Undoable Counter</h1>
<div class="undo__Redo__Container">
<button class="undoBtn">undo</button>
<button class="redoBtn" disabled>redo</button>
</div>
<div class="main__area">
<button class="numberBtn">-100</button>
<button class="numberBtn">-10</button>
<button class="numberBtn">-1</button>
<p class="result">0</p>
<button class="numberBtn">+1</button>
<button class="numberBtn">+10</button>
<button class="numberBtn">+100</button>
</div>
<div class="main__History">
<h1>History</h1>
<div class="history__container"></div>
</div>
</div>
<script src="src/index.js"></script>
</body>
</html>
@import url("https://fonts.googleapis.com/css2?family=Indie+Flower&display=swap");
* {
font-family: "Indie Flower", cursive;
}
.app__container {
/* margin: auto; */
/* width: 16%; */
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
.undo__Redo__Container {
display: flex;
flex-direction: row;
}
.undoBtn,
.redoBtn {
width: 10em;
height: 3em;
margin: 10px;
border: 2px solid black;
font-weight: 700;
font-size: 1em;
cursor: pointer;
}
.main__area {
display: flex;
flex-direction: row;
}
.numberBtn {
width: 5em;
height: 3em;
margin: 10px;
border: 2px solid black;
cursor: pointer;
}
.numberBtn,
.result {
font-size: 1.5em;
}
.result {
padding: 20px;
}
button:disabled {
border: none;
}
.main__History {
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
}
.history__container {
display: flex;
flex-direction: column;
border: 2px solid black;
width: 100%;
}
.logElements {
padding: 5px;
font-size: 1.6em;
}
const undoBtn = document.querySelector(".undoBtn");
const redoBtn = document.querySelector(".redoBtn");
const numberBtn = document.querySelectorAll(".numberBtn");
const result = document.querySelector(".result");
const historyContainer = document.querySelector(".history__container");
const redoNode = []; //on clicking of redoButton
const undoNode = []; //on clicking of undoButton(for node)
const redoArray = []; //on clicking of redoButton
const undoArray = []; //on clicking of undoButton(for numbers)
numberBtn.forEach((ele) => {
ele.addEventListener("click", () => {
let node = ele;
showResult(node);
});
});
undoBtn.addEventListener("click", () => {
undoButton();
});
redoBtn.addEventListener("click", () => {
redoButton();
});
function showResult(node) {
let number = parseInt(node.textContent.slice(1, node.textContent.length));
let sign = node.textContent[0];
let answer = 0;
const historyElement = document.createElement("div");
historyElement.className = "logElements";
const nodeTextContent = `${node.textContent}`;
let beforeResult = result.textContent;
if (sign === "-") {
answer = parseInt(result.textContent) - number;
} else {
answer = parseInt(result.textContent) + number;
}
historyElement.append(nodeTextContent, ` (${beforeResult}-->${answer})`);
historyContainer.appendChild(historyElement);
// redoArray.push(answer);
undoArray.push(beforeResult);
undoNode.push(historyElement);
result.innerText = answer;
// console.log("redoArray :", redoArray);
// console.log("undoArray : ", undoArray);
// console.log("undoNode :", undoNode);
}
function undoButton() {
redoBtn.disabled = false;
let lastElementOfUndo = result.textContent;
redoArray.push(lastElementOfUndo);
// console.log("lastElementOfUndo :", lastElementOfUndo);
// console.log("redoArray :", redoArray);
if (undoArray.length > 0) {
let element = undoArray.pop();
if (element !== undefined) {
result.innerText = element;
historyContainer.removeChild(historyContainer.lastElementChild);
}
let beforeEle = undoNode.pop();
redoNode.push(beforeEle);
}
console.log("undoArray : ", undoArray);
}
function redoButton() {
let lastElementOfRedo = result.textContent;
undoArray.push(lastElementOfRedo);
// console.log("lastElementOfRedo :", lastElementOfRedo);
// console.log("undoArray :", undoArray);
if (redoArray.length > 0) {
let element = redoArray.pop();
if (element > 0) {
result.innerText = element;
let afterEle = redoNode.shift();
undoNode.push(afterEle);
historyContainer.appendChild(afterEle);
}
}
if (redoArray.length === 0) {
redoBtn.disabled = true;
}
// console.log("redoArray : ", redoArray);
// console.log("redoNode : ", redoNode);
}
This Pen doesn't use any external CSS resources.
This Pen doesn't use any external JavaScript resources.