# Basic Algorithm Scripting - Chunky Monkey

Tell us what’s happening:
Hello. please I don’t understand the solutions for the chunky monkey exercise. I find it difficult to connect the workings of the loop and the slice. I hope someone is willing to explain. thanks
ps ignore the comments I made in the code, doesn’t make sense.
the code I have was after checking the solutions but I still don’t get how it works.

1. List item

i

``````function chunkArrayInGroups(arr, size) {
const newAr = []
{

for(let i=0;i<arr.length;i+=size){
newAr.push(arr.slice(i,i+size))
}

}
return newAr;
}

console.log(chunkArrayInGroups(["a", "b", "c", "d"], 2));
// create an emplty array
//loop through arr. regular ffor loop
// create a new empty array
//push the numbers from arr into the new empty array
//based of the size
//push the ne empty array tptonew arr
// return new arr
``````

User Agent is: `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36`

Challenge: Basic Algorithm Scripting - Chunky Monkey

You don’t need the extra `{` and `}` you wrapped the for loop in. Copy and paste the code below (which is what you have above (minus the unnecessary curly braces) with console.log statements showing you what the values of all the variables are at each iteration of the for loop. This will allow you to see what is happening at each step.

``````function chunkArrayInGroups(arr, size) {
console.log(`\nchunkArrayInGroups(\${JSON.stringify(arr)}, \${size})`);
const newAr = []
for(let i=0;i<arr.length;i+=size){
console.log(`i = \${i}, i + size = \${i + size}, arr.slice(\${i}, \${i + size}) = \${JSON.stringify(arr.slice(i,i+size))}`);
newAr.push(arr.slice(i,i+size))
console.log(`newAr = \${JSON.stringify(newAr)}`)
}
return newAr;
}
``````

thank you for your response. Please could you break down the code? I don’t understand how it works. thank you

Which part?

If you run the code with the console.log statements, you will see how it is working and what each part does. If you look at solution #2 in the guide, you will see the following explanation for the code: