JS noob here, trying to make Tetris. I have an HTML collection "squares" of all of the grid squares that make up the playable game field defined with
let squares = Array.from(document.querySelectorAll('.grid div'))
where grid is a class in the corresponding HTML. This array is filled with 200 div objects for a 10x20 grid. When a tetromino passes through one of the grid squares, the class "tetromino" is added to the square as such:
current.forEach(index => {
squares[currentPosition+index].classList.add('tetromino')
})
where current
is an array with elements that specify the relative position of the tetromino squares on the grid and currentPosition
tracks absolute position of a single square in the tetromino.
When I then go to remove the tetromino class:
current.forEach(index => {
squares[currentPosition + index].classList.remove('tetromino')
})
Each div object that had the tetromino class is then removed from the squares
array, or the index that it was at is undefined. I would expect the class to be removed but the object to remain in the squares
array. What is going on?