I have the following function to rotate a matrix 90deg and this is the code
function rotateMatrix(array) {
let counter = 0;
let resultArr = array.slice();
let i = 0,
k = 0,
p = 0;
j = array.length - 1;
console.log(array === resultArr); //false
while (counter <= Math.pow(array.length, 2)) {
if (i < array.length) {
resultArr[k][p] = array[i][j];
i++;
p++;
} else {
j--;
k++;
i = 0;
p = 0;
}
}
return resultArr;
}
Even though I created a copy of the array whenever I try to mutate the resultArr to insert the values for the rotated matrix, both of the arrays (resultArr & array) gets mutated, when I compare (resultArr === array) it gives me false.
As you can see here: Capture of both arrays in the debbuger
Does anyone have an idea why is this happening?