Slice issue with Chunky Monkey

I’m stuck on the Chunky Monkey problem. Below is my code so far. Any hints or ideas on how I can get the 2nd iteration of slice to return into my new array?

function chunkArrayInGroups(arr, size) {
   let newArr = [];
	 
  
 for(let i =0; i < arr.length; i += size){
      //console.log(arr.slice(i));
      let tempArr = arr.slice(i,size);
   	  console.log(tempArr)
			if(tempArr.length == size){
        newArr.push(arr.slice(i,size));
			}
      else{
			  newArr.push(arr.slice(i));
				break;
			}
  }
  return newArr; 
}

What is ‘the second iteration of slice’? I don’t exactly understand what you think is happening?

Are you using slice correctly?

When slice is called for the 2nd time. The method gives me an empty array. I know why……….however I don’t how to use a for loop and the slice method to obtain the items I need for the mew array. Does that make sense?

Why does the slice give you an empty array then? That’s useful knowledge to mention if you figured it out!

1 Like

I’m starting at the 2nd index and slicing two items …problem is my starting index is behind the number items I need to slice. I should be slicing 4 items in my 2nd loop….than 6 items in my final loop. I really don’t know how to iterate the slice method. Does that make sense?

How are you moving forward the start value? How far after the start value do you want the stop value?

1 Like

I want to increase i by two and keep going till I’m larger than arr.length… I could add i to size for each incrememt…that could work….

1 Like