Chunky Monkey - Appears to be correct but doesn't pass

Chunky Monkey - Appears to be correct but doesn't pass
0

#1

Hi everyone, I’m wondering if anyone can tell me why my code isn’t passing any of the tests?

So I tested it on the chrome console and it looks like it achieves the desired result.

function chunkArrayInGroups(arr, size) {
 let spliced;
 let finalArr = [];
   while (arr.length > size) {
    spliced = arr.splice(0, size);
    finalArr.push([]);
    finalArr[finalArr.length-1].push(spliced);
  }
    if (arr.length <= size) {
      finalArr.push([]);
      finalArr[finalArr.length-1].push(arr);
    }
  return finalArr;
}
chunkArrayInGroups([0, 1, 2, 3, 4, 5, 6, 7, 8], 4);

This is the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-algorithm-scripting/chunky-monkey/


#2

Wait: so do you still have a problem?


#3

No, I figured out I was doing this all wrong! Sorry.


#4

Then, you should click your own reply as the “solution.”


#5

Ok so now that I feel like I’ve understood what I should have done I can’t pass any of the tests either, even though it seems to work on the chrome dev tools console :confused:

I’m gonna update the OP.

function chunkArrayInGroups(arr, size) {
 let spliced;
 let finalArr = [];

  while (arr.length > size) {
    spliced = arr.splice(0, size);
    finalArr.push([]);
    finalArr[finalArr.length-1].push(spliced);
  }
    if (arr.length <= size) {
      finalArr.push([]);
      finalArr[finalArr.length-1].push(arr);
    }
  return finalArr;
}
chunkArrayInGroups([0, 1, 2, 3, 4, 5, 6, 7, 8], 4);

#7

Ok, I figured it out, looks like I was adding an additional array level by pushing [] into the final array every time… It made sense in my head somehow xD