(I have looked at similar questions that were asked before, but I couldn't find a satisfactory answer... my apologies!)
Hey everyone,
I am currently using a nested for-loop which is resulting in new arrays being created.
Each time the inner loop executes, I intend to add the generated array into an empty pre-defined array. My goal is to have an array of arrays as the final output.
While testing by console-logging each array generated within the inner loop, all the expected arrays are indeed being createdđź‘Ť. However, strangely, when I attempt to use .push() to include each one in the empty array, I end up with an array filled with index numbers instead of an array of arrays (???) .
If anyone understood that explanation, please lend a hand!! Thanks a bunch!
const isColourful = num => {
const arr = num.toString().split('');
const blankArr = [];
const arrayOfArraysMaker = arr => {
for (let c = 0; c < arr.length; c++) {
for (let i = 0; i < arr.length; i++) {
if (i + c > 0) {
x = i + c;
} else {
x = undefined;
}
const workingArr = blankArr.push(arr.slice(i, x));
console.log(blankArr);
}
}
};
arrayOfArraysMaker(arr);
return arr;
};
console.log(isColourful(2457));
This code snippet returns a list of numbers from 1 to 16, while I was actually expecting an array containing the following arrays:
[ '2', '4', '5', '7' ]
[]
[]
[]
[ '2' ]
[ '4' ]
[ '5' ]
[ '7' ]
[ '2', '4' ]
[ '4', '5' ]
[ '5', '7' ]
[ '7' ]
[ '2', '4', '5' ]
[ '4', '5', '7' ]
[ '5', '7' ]
[ '7' ]