I was learning javascript right now and I came across this question:
What i want to do is takes 3 number as input and generate a 3d array based on the input. Populate each cell in the array with the product of the indexes.
I can't figure it out. Appreciate your help
CodePudding user response:
Here's an approach
function create3DArray (a, b, c) {
const matrix = [];
for (let i = 0; i < a; i ) {
matrix[i] = [];
for (let j = 0; j < b; j ) {
matrix[i][j] = [];
for (let k = 0; k < c; k ) {
matrix[i][j][k] = i * j * k;
}
}
}
return matrix;
}
console.log(create3DArray(2, 2, 2));