I've worked with Javascript for over 3 years and I haven't encountered this behaviour before. I'm completely new to Node.js though, so maybe there's something I'm missing.
I have the following nested for loop to fill an array called "result".
let result = new Array(nRows1).fill(new Array(nCols2).fill(0.0));
console.log("init: ", result);
for (let p = 0; p<nRows1; p++){
for (let q = 0; q<nCols2; q++){
let test = vectorDotProduct(matx1[p], matxT[q]);
result[p][q] = test;
console.log(result[p][q]);
}
}
console.log(result);
Here is the output of the console:
init: (3) [Array(1), Array(1), Array(1)]
0: [0.08607474528466699]
1: [0.08607474528466699]
2: [0.08607474528466699]
0.2229019646991845
0.08090946281136112
0.08607474528466699
(3) [Array(1), Array(1), Array(1)]
0: [0.08607474528466699]
1: [0.08607474528466699]
2: [0.08607474528466699]
Q1: Not that it matters, but why isn't new Array().fill() returning zero values? Q2: Why does the array seems to get overwritten with the value in result[2][0] once the for loop is done?
I tried stepping through manually, and everytime it would work fine until we exit the for loop and the array gets overwritten with the last value in it. I feel like I'm being stupid but I just can't see why it's not working.
extra info: for this particular test, nRows1 is 3 and nCols2 is 1. result will be a 3x1 column vector.
Any help would be greatly appreciated!