Chunky Monkey isn't working

Hi any clue why this isn’t working? Thanks

Your code so far

function chunkArrayInGroups(arr, size) {
 var result = [];
 var sliced = arr.slice(size);
 for (let i = 0; i < arr.length; i++) {
   if (arr[i] < size) {
return result;

console.log(chunkArrayInGroups(["a", "b", "c", "d"], 2)); 
   **Your browser information:**

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36.

Challenge: Chunky Monkey

Link to the challenge:

Hi @am93
Looks like you are off by a mile. I am not sure if you have understood the problem you are dealing with here. Something like

  1. chunkArrayInGroups(["a", "b", "c", "d"], 2) should return [["a", "b"], ["c", "d"]]
  2. chunkArrayInGroups([0, 1, 2, 3, 4, 5, 6], 3) should return [[0, 1, 2], [3, 4, 5], [6]]

I suggest you console what you are returning and invoke the function with the parameters provided in the test description. You will see where you are with this.

Yes I know those are supposed to be the answers. I have also console logged stuff.

I had initially tried solving starting with something like

var firstD = arr.slice(0 , size);
var secondD = arr.slice(size);

But couldn’t figure out how to make it one array. Then I saw in the hint I was supposed to use a for loop and now I’m here

You can use slice to answer the question quite easily. Look at the hint below.

function chunkArrayInGroups(arr, size) {
 var result = [];
 for (let i = 0; i < arr.length; i += size) {
   // Slice here and push in result.
 return result;

What you need to figure out is slice from what index to what index.

Why is it i += size? Looking at chunkArrayInGroups([0, 1, 2, 3, 4, 5], 2) I’d be returning [ 0, 2, 4 ]

I don’t usually try to code at night, I guess I’ll look at it tomorrow since the question can be answered “quite easily”. Maybe I’m tired

Not really. chunkArrayInGroups([0, 1, 2, 3, 4, 5], 2) should be returning
[[0, 1], [2, 3], [4, 5]]. The clue here is in “chunking” (grouping) elements in the provided array into arrays of length equal to second argument.

Hi, I’m not sure what to do. I’m here expecting that the for loop increases by size and slices in this case with index 0,2 [0,1] → 2, 4 [2,3] → 4,6 [4,5], then pushing arr into result array and returning result.

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

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

slice takes numbers as argument, not arrays

Ok I don’t know what to do here. I tried creating a variable for arr[i] and using the variable nam but it’s not working

Just curious could my code have worked as two lines to have the same effect as result.push(arr.slice(i, i + size));

You can try result.push(arr.slice(i, i + size)).

Yeah sorry that’s what I meant, just got that one from the solution so it had an incorrect name. Is there a two line method that could’ve worked?

By two line method, do you mean solving the challenge in two lines of code?

You can probably do it in one (long) line, if you use .reduce.

Yes because that’s where my rookie skillset had led me

There are two parts to solving these challenges

  1. Coming up with the logic to solve the problem

  2. Expressing that logic in code

As far as I’m aware, all of these challenges can be solved with if, for and while. It is perfectly fine to restrict yourself to syntax you are comfortable with and then refactor to more idomatic syntax once your solution works.

do you mean this code below?

as I said, slice doesn’t like to have an array as argument, that’s one issue

the second one, the second argument is the final index of the copied array, it can’t always be the same

Don’t get me wrong but it seems a bit like when you see a challenge, you immediately start hacking away with only a faint plan of how it “should probably work”. If you get it done, it’s fine (pure luck though, not skill), and if you don’t, you look at the solution.
Not trying to put you in a corner but that’s sort of the vibe I’m getting.

As for this challenge, your general concept is perfectly fine. You have a result array, and then you go through a loop and push certain parts of the original array in there. You just haven’t thought it through.

Take a pen and paper and write down the indices that the slice method needs in each step. It takes numbers, nothing else.

  • step 1: from 0 to 2
  • step 2: from 2 to 4
  • step 3: from 4 to 6 (no wait, the array is only 4 items long)

You can make this work with different strategies, and different loop parameters. But you ultimately need to figure out the above first, before you should even think about starting to write code.

1 Like

@jsdisco I don’t think you know enough to make that judgement as to what I do. Theres actually a ton of posts I made so you can look into it if you want. I comment my thought process, look up the functions I need, then make an attempt. Once I can’t come to a solution I ask for help.

Right below I even commented what I tried to do

Be very careful about negative comments. Even if that’s not your intent, it’s not productive. Personally I think reading @nibble say “looks like you’re off by a mile” after after an hour attempt to solve a problem wasn’t the best either.

Since none of you have a magic wand that determines the effort someone puts in code, don’t jump to conclusions. I personally don’t care but there’s a ton of people who just quit coding when they get discouraged. Or maybe continue without posting questions like the guy who commented here he’s coded for a year and wants to quit.

Wow what a guilt trip. I don’t think I can help you beyond

  • slice doesn’t change the original array. If you want to slice something out of an array and put it somewhere else, you’ll have to assign it to a new variable
  • slice needs the correct indices (numbers), which you can retrieve from combining the i index of the for loop and the size given (this depends on the intended logic of your for loop)

Good luck mate.