From dba3c7f55c4fe53aae4015eae23147dd8656e0fd Mon Sep 17 00:00:00 2001 From: Inga Date: Wed, 20 Nov 2024 14:32:22 +0000 Subject: [PATCH] fixed a bug with handling changing board sizes when size is decreased more by 1 row / column --- src/frontend/components/board-game.ts | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/src/frontend/components/board-game.ts b/src/frontend/components/board-game.ts index 8109020..ac8f7b5 100644 --- a/src/frontend/components/board-game.ts +++ b/src/frontend/components/board-game.ts @@ -49,11 +49,11 @@ export class BoardGameComponent extends HTMLElement { for (const tbodyUntyped of this.querySelectorAll("tbody.game-board")) { const tbody = tbodyUntyped as HTMLTableSectionElement; - for (let rowNumberToRemove = gameState.rows; rowNumberToRemove < tbody.rows.length; rowNumberToRemove++) { - tbody.rows[rowNumberToRemove]?.remove(); + while (gameState.rows < tbody.rows.length) { + tbody.rows[0]?.remove(); } - for (let rowNumberToAdd = tbody.rows.length; rowNumberToAdd < gameState.rows; rowNumberToAdd++) { + while (gameState.rows > tbody.rows.length) { tbody.insertRow(); } @@ -63,15 +63,11 @@ export class BoardGameComponent extends HTMLElement { continue; } - for ( - let columnNumberToRemove = gameState.columns; - columnNumberToRemove < row.cells.length; - columnNumberToRemove++ - ) { - row.cells[columnNumberToRemove]?.remove(); + while (gameState.columns < row.cells.length) { + row.cells[0]?.remove(); } - for (let columnNumberToAdd = row.cells.length; columnNumberToAdd < gameState.columns; columnNumberToAdd++) { + while (gameState.columns > row.cells.length) { row.insertCell(); }