In my coding project, I came across a Matrix stored in a 2D-array:
var testM = [
[0.0,2.0,3.0],
[1.0,1.0,1.0],
[7.0,5.0,6.0]
];
// execute row exchange between a and b
function swapRows(a,b){
let temp = a.slice(); // Cloning the value directly rather than referencing
a = b.slice();
b = temp;
}
// When passing individual arrays:
swapRows(testM[0], testM[1]);
Despite running the code above, the original 2D array remains unchanged. Since I'm not well-versed in how JavaScript handles reference passing, any insight would be helpful. (It functions correctly when inserted inline, so it's puzzling why it doesn't work even with cloning by value)
UPDATE: I grasp the concept of swapping elements within a standard one-dimensional array; my interest lies more in understanding how JavaScript manages reference passing. (Not entirely sure if I'm using the correct terminology here).