Chunky Monkey, Monkey Chunky

Hey guys! Having some trouble trying to make the rest of the arrays split into the size of “size”. I have got the first array to be the size of “size”, but the rest of the characters are all put into one array. I also don’t know where to implement the loop. Thanks!


function chunkArrayInGroups(arr, size) {
let newArr=[]
let result=0;

for (let i=0;i<arr.length;i++) {

result=arr.splice(0,size)
newArr.push(result)



}
return newArr
}

console.log(chunkArrayInGroups(["a", "b", "c", "d"], 2));

Your browser information:

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

Challenge: Chunky Monkey

Link to the challenge:

Since splice changes the array and you are still incrementing i at each iteration over the original array, you will have unexpected results. Try using slice instead.

1 Like

Am I using the loop correctly? I have tried using slice but it keeps giving me the first array 4 times over. Do I need to use [i] anywhere for this implementation? Thanks!

You will have to specify the index (i) at which you want to capture the next slice. This means when you update i at each iteration of the for loop, you need to make sure it has the applicable value.

Note: The second argument passed to slice specifies the index to end (up to but not including).

1 Like

I tried using i++ and if I use [i], it has to be on arr right? I tried size[i] and got some weird results. Here is what I have so far but it’s just returning the original array. It’s a bit hard grasping the application of [i] and how it will all come together.


function chunkArrayInGroups(arr, size) {
let newArr=[]
let result=0;

for (let i=0;i<arr.length;i++) {

result=arr[i].slice(0,size)
i++
newArr.push(result)



}
return newArr
}

console.log(chunkArrayInGroups(["a", "b", "c", "d"], 2));

You should be using slice on the array. You are trying to use it on the ith element of the array. Once you fix that, your slice is still starting at index 0 up to and not including index 2. Use i as the index you want to start and then make sure you adjust i at each iteration to account for size.

Also, currently you are increasing i by one in two places. Just do it in the for loop declaration. Also, you don’t just want to increment it by one. Think about what you should be incrementing it by.

1 Like

I have to figure out the adjustment I have to make to i after each iteration. And I don’t increment by one (i++), I have tried i+size, or size++. Both yield wrong results. Will try a little longer on this one


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

You need to adjust two things:

  1. The i used in the for loop declaration
  2. The second argument of the slice method
2 Likes

I appreciate you walking me through it. I had forgotten that you can use more than just the i++ or i-- operators in the loop. I googled different operators you can use to increment with (stackoverflow) and discovered the += operator. I used that in conjunction with size which allowed me to use size in the declaration (which I previously though was impossible). Then I fiddled with the second argument until I got to size+i. Which is still foggy to me, it was the console that helped me on that one. I really need to review some stuff…

Here is the passing code:


function chunkArrayInGroups(arr, size) {
let newArr=[]
let result=0;

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

newArr.push(result)



}
return newArr
}

console.log(chunkArrayInGroups(["a", "b", "c", "d"], 2));
let result = 0;

This line is not needed. Instead, you can simply declare it inside the for loop:

const result = arr.slice(i, size + i)
1 Like

Recursive solution…

const chunkArrayInGroups = (arr, s, full = []) => {
 return (arr.length === 0 ) ?
    full :
    chunkArrayInGroups(arr, s,[...full, arr.splice(0,s)] )
}

Please do not revive old topics just to post your solution.