# Basic, Algorithm Scripting: Chunky Monkey

Basic, Algorithm Scripting: Chunky Monkey
0

#1

Good Morning Good and Gentle people of Dulac !

I come seeking knowledge of where it is that this fails. I read through the code… and the code that I have put into Codepen works, with all the challenges, but something in the test is not being accepted and I have not found it yet. Here is my code :

``````function chunkArrayInGroups(arr, size) {
let tempArr = [];

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

tempArr.push(myChunk);
return tempArr;
}
}

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

#2

You’re returning `tempArr` too early, inside the for-loop. You probably intended to `return` after the for-loop has finished running

#3

If you don’t know, there’s a tool which allows you to “visualize” the execution of your code through it getting stepped through, and keeps track of all of the information. Just click on the tab and change it to JavaScript. I think it could have helped you here.

In this case, let’s suppose we had to step through the code step by step by hand. That may sound daunting, but it can often help (and it probably helps to encourage to keep your code modular).

With your example, first [“a”, “b”, “c”, “d”] gets binded to arr and 2 gets binded to size. Then [] gets binded to tempArr. Then 0 gets binded to i. Then arr gets sliced with indexes (0, and 2), so that’s [“a”, “b”] and binded to arr. Then tempArr gets pushed onto myChunk. So, tempArr has value [[“a”, “b”]]. Then, and we’re still inside of the for loop, tempArr gets returned.

#4

I also ran through your code and found out a solution with some more code.

There’s one thing to notice about your code that I think gets frowned upon by more experienced designers. You have arr.length in the termination condition for the ‘for loop’. However, you keep splicing that arr, and thus the value of arr keeps changing. So, what to do?

I would suggest that you make a clone of arr and then use that for your upper bound. By ‘clone of arr’, I mean a copy of arr that doesn’t get change when you change arr. Actually, it’s probably less code to find the value of arr.length before the computer would compute the ‘for loop’, store it as a constant, and then execute the ‘for loop’.

When do you want the computer to return tempArr? I ended up writing a conditional for that, but I suggest you think about that.

Also, there’s another way to solve this problem without using slice at all. The first time I solved it, and it’s not a more efficient solution, I first wrote a helper function to create an array of empty arrays. Then I called that function on the ceiling of the ratio of the length of the array and size. So, if the ratio was 2.5, I’d have an array like [[], [], []]. Then I pushed each number from the original array into the appropriate subarray. My function had two for loops though, so on average it would end up slower than the track that you’re on.

#5

He is slicing the array, not splicing. Slice doesn’t affect the original array.

#6

You’re right. I ended up writing a solution using “splice” instead of “slice”. I’m not sure why I did that. Thanks!