# Chunky Monkey, where is the difference!?

hi campers

I’ve been a long time fighting to solve this challenge I’m trying every piece of code coming to my mind until I was really near to solve it then I’ve looked for people that solve that challenge before, anyway what I want to ask you about, is just why these pieces of code are the same but the first one doesn’t work rather than the second, where do you think the problem is?

the first one which doesn’t work:

``````
function chunkArrayInGroups(arr, size) {
// Break it up.
let newArr = [];
for(let i = 0; i < arr.length / size; i++){
let r = arr.splice(0, size);
newArr.push(r);
}
return newArr;
}

chunkArrayInGroups(["a", "b", "c", "d"], 2);
``````

the second one which passes all the tests:

``````
function chunkArrayInGroups(arr, size) {
// Break it up.
let newArr = [];
let len = arr.length / size;
for(let i = 0; i < len; i++){
let r = arr.splice(0, size);
newArr.push(r);
}
return newArr;
}

chunkArrayInGroups(["a", "b", "c", "d"], 2);
``````

they seem similar but why the first one doesn’t even if they’re the same logicaly.

In the first code, since you check if i < arr.length / size, on the start of the 3rd iteration, i would be 2 and arr.length /size would be 0.5, so it would not continue.

1 Like

Maybe my thinking is off (I only got 3.5 hours sleep last night). but when I use the following to check the values:

``````function chunkArrayInGroups(arr, size) {
// Break it up.
let newArr = [];
console.log('i starts as 0');
console.log('arr.length /size starts as ' + arr.length / size);
console.log()
for (let i = 0; i < arr.length / size; i++) {
let r = arr.splice(0, size);
newArr.push(r);
console.log('next i will be ' + (i + 1));
console.log('next arr.length / size will be ' + arr.length / size);
console.log()
}
return newArr;
}

chunkArrayInGroups(["a", "b", "c", "d", "e"], 2);
``````

I get the following in the console:

``````i starts as 0
arr.length /size starts as 2.5

next i will be 1
next arr.length / size will be 1.5

next i will be 2
next arr.length / size will be 0.5
``````

The last values shown above are what will be used to evaluate the for loop condition, so there is no third execution of the inner for loop code block.

1 Like

Oh yes. Sorry for the confusion. When I first looked at the user’s code, I assumed they were getting the correct answer (did not test code) for even number of elements, but failing on odd number of elements, so I added the extra element to show why.

When I run the same code above for the original example of 4 elements, it does exactly what you stated.

``````i starts as 0
arr.length /size starts as 2

next i will be 1
next arr.length / size will be 1
``````
1 Like

Was working on something for a client and was testing out something new I had not used before.

1 Like

Aah okay

Thank you so much, guys,  what I understand is the array (arr) become short after the mutation pf splice() method and the value or arr.length / size become less by one after each iteration

is that exactly what I need to understand?