I’m trying to build a battleship game, the fleet is a 3d array contains the coordinates of ships
I’m trying to get the second cell of the second ship, but I keep getting empty arrays!
how to do this though I’m not sure if slice method here is the perfect way to do this
Why are you using .slice? You only need to use array subscripting, like so:
let x = arr
I need to delete the cell so later I can check if all cells of ship are hit
//all cells are removed -> the ship is sinked
So you’re wanting to get the cell [6,6] right?
let x = arr.slice(0,2);
console.log(x) // gives [6,6]
And say you wanted cell [1,3] you would do
But slice just returns a copy to a new array object. You’re wanting to delete the cell right? To do that just use splice.
console.log(arr); // returns [[[1,2],[1,3]],[[5,6]]]