I have a matrix of arrays and I want to populate each array with different random numbers, I've tried to use fill() method, but it set only one number for each array, while I want to set all different numbers. Here is the link on my pen and the code I've problem with:
let matrix = [];
function matrixItem() {
let a = +prompt("How many arrays should matrix include?");
let reg = /^\d+$/;
if (reg.test(a) && (typeof (a)) != null && a != '' && a <= 10) {
for (let i = 0; i < a; i++) {
matrix.push((Array(Math.floor(Math.random() * 5) + 1)
/* problem is here */.fill(Math.round(Math.random() * 100))));
}
let sum = matrix.map(function (x) {
return x.reduce(function (a, b) {
return a + b;
});
});
console.log(sum);
} else matrixItem();
}
matrixItem();
Any help would be appreciated.
fill()method fills all the elements of an array with a static value. UseArray.frominstead