I have the following matrix
var matrix = [
[2, 0, 0, 2],
[4, 0, 0, 2],
[2, 64, 32, 4],
[1024, 1024, 64, 0]
];
And I would like to shift the zeros in the second and third column of first and second row to the end, but I don't know how to do it. Here is my attempt:
for(var i = 0; i < matrix.length; i++) {
for(var j = 0; j < matrix.length ;j++) {
//push zeroes to the end
if(matrix[j][i] === 0){
for(var k = j+1; k<2;k++){
matrix[j][i] = matrix[k][i]
}
matrix[3][i] = 0
}
}
}
This is what it returns vs. what I expected:
Returns: [[2, 0, 0, 2], [4, 0, 0, 2], [2, 64, 32, 4], [1024, 0, 0, 0]]
Expected: [[2, 64, 32, 2], [4, 1024, 64, 2], [2, 0, 0, 4], [1024, 0, 0, 0]]
I would really appreciate any help