when I am using the spread operator to get the element from the variable "matrix" into the empty array of "result" the spread operator is getting all the elements of the matrix and putting them in the "result" array but when I am performing a looping action on the array it is not giving me the correct output, but when I am not using the spread operator and giving the result a normal array, I'm getting the desired output. Why is this happening?
function rotateImage(n, matrix) { let result = [...matrix]; let count = 0 for (let i = matrix.length - 1; i >= 0 ; i--) { for (let j = 0; j < matrix.length; j++) { result[j][i] = matrix[count][j]; count j i } count++ if(count == 3){ count = 2; }; } return result; } The above is not working.
But the below is giving the desired output.
function rotateImage(n, matrix) { let result = [[1,2,3],[4,5,6],[7,8,9]]; let count = 0 for (let i = matrix.length - 1; i >= 0 ; i--) { for (let j = 0; j < matrix.length; j++) { result[j][i] = matrix[count][j]; count j i } count++ if(count == 3){ count = 2; }; } return result; } Why is that?