Tell us what’s happening:
So, here is my inelegant solution. Could I get some constructive feedback?
The solution forum is unlocked but I can’t seem to respond.
Your code so far
function chunkArrayInGroups(arr, size) {
let newArr = [];
let nestArr = [];
if (size < 1) { return arr}; // Ensure size cannot be less than 1, preventing infinite loop.
for (let i=0; i <= arr.length; i++) {
while (nestArr.length < size) {
let shiftValue = arr.shift(); // Value = arr[0]
if (shiftValue === undefined) { // If no elements remain
break; // Stop the loop
} else {
nestArr.push(shiftValue); // Otherwise push the value to the nestArr so long as the nestArr is less than the 'size' attribute.
}
}
newArr.push(nestArr); // Finally, push the nestArr to newArr.
nestArr = []; // Reset nestArr for next iteration.
i=0; // Reset i, as the original arr.length has changed.
}
console.log(newArr)
return newArr;
}
chunkArrayInGroups([0, 1, 2, 3, 4, 5, 6], 3);
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36
Challenge Information:
Basic Algorithm Scripting - Chunky Monkey