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++){

  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

You do not have to use the slice function to solve this challenge, but it does make it easier. Write out your algorithm in plain language (no code) and make sure it will result in the correct answer as you walk through each step. Then, you can start to write your code and use whatever JavaScript functions you know to implement your algorithm.

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)
        if(tempArray === size){

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.


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

The above line has a syntax error. Looks like you have an extra space where it does not belong.

Also, you are missing the function declaration showing the parameters arr and size.

Your function should use a return statement to return the final expected array.

In the following line you attempt to use push on tempArray, but you do not actual call the function with a value ( missing (aValue) ).


Finally, the following line attempts to increment a count variable by 1, but you have not defined a starting value for count.


Fix these issues and you may or may not solve this challenge, but you will at least not have as many errors.

You really should write out your entire algorithm in plain language (no code) first, and make sure the steps you have written would result in the answer each tests expects. Once you feel your algorithm generates the correct answers, then (and only then) should you attempt to write code. As you are writing your coded solution, use your browser’s console (Ctrl+Shft+J in Chrome) to check for any errors your code may create.