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);
Thank you in advance for your kind assistance.
Youāre returning tempArr too early, inside the for-loop. You probably intended to return after the for-loop has finished running
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.
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.
He is slicing the array, not splicing. Slice doesnāt affect the original array.
Youāre right. I ended up writing a solution using āspliceā instead of āsliceā. Iām not sure why I did that. Thanks!