Chunky monkey code programming logic help

Hey Everyone

I need help understanding the logic of Chunky Monkey coding challenge. I don’t understand how slice() and push() can be used together. Can someone give me a hint or lead me into the right direction? I’ve been on this for a few hours. Below is the code I have. Thank you.

  
  var newArray = [];
  var newArray2 = [];
  var count;
  
  for(var i = 0; i < arr.length; i++){
     newArray.push(arr[i]);  
         
  }
  

  
  return newArray;
}

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

Right now you’re just copying every element from arr into newArray. Before trying to write code, figure out the logic of how you’re going to accomplish this task.

That’s the thing I usually write out the problem beforehand and fill in the blankets. But I don’t know how to use the slice method.

For example. I write a for loop to iterate and push() it into a newarray. When I get to every [size] element I know I’m supposed to stop. This will create my new sub array. From there I start the process again until there is no more elements. Problem is I don’t know how I’m using the slice() method for this problem.

1 Like

I figured out how to use the slice() method. I understand its making a new array when you slice the original array, but here is the problem.

Once I start my for loop to iterate in the arr. How do I know when to slice arr? Do I slice each index in the array and then place in a temp array and insert into a newArray? I’ll show what I had that didn’t work for me. It’s only pseudo code but it will give an idea on what I have.

var newArray = []
var tempArray = []
var counter = 1
var tempNum

for( var i = 0; i < arr.length; i++){
          if(tempArray.length < = size){
             temp = arr.slice(i,counter)
             tempArray.push
             count++
          }
        if(tempArray === size){
            newArray.push(tempArray)
       }
}

This is what I have so far its very rough but its not working at all. I’ve been working on this for awhile(6 hours) and I don’t want to look at the answer. Any hints or ideas would help me greatly.

I’ve edited your post for readability. When you enter a code block into the forum, precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

markdown_Forums

So you want code blocks as well for pseduo code?..