Skip to content

Commit

Permalink
fix: square level update
Browse files Browse the repository at this point in the history
  • Loading branch information
sameemul-haque committed Apr 10, 2024
1 parent d3eb2a8 commit a5cb117
Showing 1 changed file with 16 additions and 20 deletions.
36 changes: 16 additions & 20 deletions public/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,29 @@ document.addEventListener("DOMContentLoaded", function () {
square.setAttribute("data-level", "0");
squares.push(square);
squaresContainer.appendChild(square);
}
square.addEventListener("click", function () {
const currentLevel = parseInt(square.getAttribute("data-level"));
increaseLevel(square, currentLevel);
});

let currentLevel = 0;
square.addEventListener("contextmenu", function (event) {
event.preventDefault();
const currentLevel = parseInt(square.getAttribute("data-level"));
decreaseLevel(square, currentLevel);
});
}

function increaseLevel() {
function increaseLevel(square, currentLevel) {
if (currentLevel < 4) {
currentLevel++;
updateSquareLevel();
const newLevel = currentLevel + 1;
square.setAttribute("data-level", newLevel);
}
}

function decreaseLevel() {
function decreaseLevel(square, currentLevel) {
if (currentLevel > 0) {
currentLevel--;
updateSquareLevel();
const newLevel = currentLevel - 1;
square.setAttribute("data-level", newLevel);
}
}

function updateSquareLevel() {
squares.forEach((square) => {
square.setAttribute("data-level", currentLevel);
});
}

squaresContainer.addEventListener("click", increaseLevel);
squaresContainer.addEventListener("contextmenu", function (event) {
event.preventDefault();
decreaseLevel();
});
});

0 comments on commit a5cb117

Please sign in to comment.