Splice vs. For Loop: where have I gone wrong?

The following is my third attempt to complete the Chunky Monkey Algorithm problem.
The way I’m reading my code it seems like the for loop is not iterating over the final value in an array… however, this only seems to be the case when splice is being used. The problem pops up when `chunkArrayInGroups([0, 1, 2, 3, 5, 6, 7], 2); ` is called, values 0 - 6 are in neat arrays, however 7 seems to remain in the mutated array called by the for loop.

Can someone point out what aspect of `splice()` or `for loops` I seem to be misunderstanding. Thanks a million!

``````
function chunkArrayInGroups(arr, size) {
let chunk = [];
for (let i = 0; i <= arr.length; i++){
function sizeLength(size){
if (arr.length >= size){
return size;
} else {
return arr.length;
}
}
let removed = arr.splice(0, sizeLength(size));
console.log('sizeLength(size):', sizeLength(size))
console.log('arr.length:', arr.length)
console.log('removed:', removed)
console.log('arr:', arr)
chunk.push(removed);
}
console.log(chunk);
return chunk;
}

chunkArrayInGroups([0, 1, 2, 3, 5, 6, 7], 2);
// chunkArrayInGroups([0, 1, 2, 3, 4, 5, 6, 7, 8], 4);
// chunkArrayInGroups([0, 1, 2, 3, 4, 5], 4);

``````

User Agent is: `Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:71.0) Gecko/20100101 Firefox/71.0`.

Challenge: Chunky Monkey

You don’t want to mutate an array that you are iterating over. The problem is that `i` is getting larger every time the loop runs and `arr.length` is getting smaller. I added a single log statement to show you what is happening:

Fascinating, I think I understand:
the value of `i` dictates if the `for loop` will run, so, in the code written, `i` becomes larger in value than `arr.length` by the end, therefore the remaining `arr` value will not be iterated through because `i` is larger than `arr.length` so it evaluates to a falsy value and will not run again. Wow, you taught me something new about `for loops`! Thank you!

1 Like

You got it! Remember that the second argument of the `for` loop is checked before every iteration. It doesn’t always have to be something like `i <= myArray.length` but it usually will be.

and that is where the creativity and functionality of code comes into play, Brilliant! Thanks again!!!

i have a few solutions for this.

Solutions
``````
//making adjustments to yours.(going in reverse in the for loop)
function chunkArrayInGroups(arr, size) {
let chunk = [];
for (let i = arr.length - 1; i > 0; i--){
let removed =  [...arr.splice(i - i, size)];
if(removed.length !==0 ){
chunk = [...chunk, removed];
}
}
return chunk;
}

function chunkArrayInGroupsV2(arr, size) {
let chunk = [];

let it = arr.length - 1;
while (it > 1) {
let partial = [...arr.splice(it - it, size)];
if (partial.length !== 0) {
chunk = [...chunk, partial];
}
it--;
}

return chunk;
}

//RECURSIVE SOLUTION
function chunkArrayInGroupsRecirsive(arr, size) {
if (arr.length <= size) {
return [arr];
} else {
return [arr.slice(0, size)].concat(
chunkArrayInGroupsV2(arr.slice(size), size)
);
}
}
chunkArrayInGroups([0, 1, 2, 3, 5, 6, 7], 2);
``````

It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.

We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.

Thank you for understanding.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.