Basic Algorithm Scripting: Chunky Monkey*


How can I use slice in a loop?

function chunkArrayInGroups(arr, size) {
        let newArr =[];
         for (let i=0;i<arr.length;i++){

          console.log(arr.slice(arr[i],size+arr[i]))  
           
           newArr.push(arr.slice(arr[i],size+arr[i]));

                      //console.log(i);
         }
   
   //console.log(newArr)

}

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

When using slice, the first argument you pass to it should be the index you want to start at and the second argument would be the index you want to slice up to but not including.

Do you understand what arr[i] represents at any given iteration?

1 Like

I can only make the first cycle works, but how can I do the second one?.

function chunkArrayInGroups(arr, size) {

 let nweArr=[];         

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

                      

            let newChunk=arr.slice(i,size-arr.length)

           i=size-arr.length

                 

                    console.log(newChunk)       

                                          

          }

                   

}

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

Yes, that represents the position where you want to start the cycle.

I still have to do these to get passed the challenge:
chunkArrayInGroups([0, 1, 2, 3, 4, 5], 3) should return [[0, 1, 2], [3, 4, 5]] .

chunkArrayInGroups([0, 1, 2, 3, 4, 5], 4) should return [[0, 1, 2, 3], [4, 5]]

chunkArrayInGroups([0, 1, 2, 3, 4, 5, 6], 3) should return [[0, 1, 2], [3, 4, 5], [6]] .

chunkArrayInGroups([0, 1, 2, 3, 4, 5, 6, 7, 8], 4) should return [[0, 1, 2, 3], [4, 5, 6, 7], [8]]

function chunkArrayInGroups(arr, size) {
 let newArr=[];         
          
          for (let i=0;i<arr.length;i++){
                      
      
         let newChunk=arr.slice(i,i+size)

        newArr.push(newChunk)                        
      
         i=i+1;
          }
         console.log(newArr)
         // return newArr
}

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

The problem you have is how you are incrementing the variable i, plus you have no return statement.

I solved hte return issue, but how can I increment I while I am in a loop?

function chunkArrayInGroups(arr, size) {
 let newArr=[];         
          
          for (let i=0;i<arr.length;i++){
                      
      
         let newChunk=arr.slice(i,i+size)

        newArr.push(newChunk)                        
      
         i=i+1;

     
          }
        
        
         console.log(newArr)
          return newArr
}

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

You are currently incrementing i in two locations. The one shown above and the line below:

i=i+1;
1 Like

It is done. Thanks!

function chunkArrayInGroups(arr, size) {
 let newArr=[];         
          
          for (let i=0;i<arr.length;i+=size){
                      
      
         let newChunk=arr.slice(i,i+size)

        newArr.push(newChunk)                        
      
         

     
          }
        
        
         console.log(newArr)
          return newArr
}

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