Write a function removeColumns that accepts two arguments:
originalGrid (two-dimensional array)
numColums (number)
removeColumns should return a new grid with the correct number of columns removed. If numColumns = 1, remove one column. If numColumns = 2, remove two columns.
removeColumns([[1, 2, 3],
[1, 2, 3],
[1, 2, 3],
[1, 2, 3]], 2);
/* => [[1],
[1],
[1],
[1]]
*/
I tried:
function removeColumns (originalGrid, numColumns) {
let newGrid = [];
//if numColumns = 1, remove 1 column
for (let col = 0; col < originalGrid.length; col ++)
if (numColumns === 1) {
newGrid.pop(col)
}
//if numColums = 2, remove 2 columns
else if (numColumns === 2) {
newGrid.pop(col + 1)
}
return newGrid;
}
And there are several issues with this.
I understand how to remove the last item from one array:
function removeItem(array) {
let poppedArray = array.pop();
return array;
}
But I do not understand how to loop through three consecutive arrays in a grid, and remove the last item from all three.
[1, 2, 3]
[1, 2, 3]
[1, 2, 3]
I think once I understand the concept of how to remove one column, I will understand how to remove two columns.
EDIT: Link to codepen