I am seeing weird behavior when trying to modify a 2-dimensional array that represents a tic-tac-toe board. The first element of all the arrays are modified instead of the one.
// BEFORE: squares = [[null,null,null],[null,null,null],[null,null,null]]
squares[0][0] = 'X';
// AFTER: squares = [["X",null,null],["X",null,null],["X",null,null]]
I was able to fix the issue with the following code, but I am curious as to why this happens. Here is the working code:
let rowToModify = squares[yCoord].slice();
rowToModify[xCoord] = 'X';
squares[yCoord] = rowToModify;
squares[0][0] = 'X';now way it can modify all the first elements. You might be doing something wrong.