Function Return Expected Output

Tell us what’s happening:
It appears to me that my function is returning the expected output. However, it is still failing. I would be very grateful if someone could put a second set of eyes on this for me and let me know where I’ve gone wrong. Thank you!!!

  **Your code so far**

function chunkArrayInGroups(arr, size) {

let foo = [];

function littleChunker(arr, size) {
  
  if (Math.ceil(arr.length/size) > 0){
    foo.push(arr.splice(0,size));
  } else {
    console.log(foo)
    return foo;
  }
  
littleChunker(arr, size);
}

littleChunker(arr, size);
}

chunkArrayInGroups(["a", "b", "c", "d"], 2);
chunkArrayInGroups([0, 1, 2, 3, 4, 5, 6, 7, 8], 2)
  **Your browser information:**

User Agent is: Mozilla/5.0 (X11; Fedora; Linux x86_64; rv:99.0) Gecko/20100101 Firefox/99.0

Challenge: Chunky Monkey

Link to the challenge:

I’m very confused why you have a function declared inside of your chunkArrayInGroups function, but regardless of that chunkArrayInGroups doesn’t return anything.

1 Like

console log the return value of chunkArrayInGroups

console.log(chunkArrayInGroups(["a", "b", "c", "d"], 2))

You need to return the inner functions return value (although I’m not sure why you needed to create a new function)

The reason why I have a function declared inside the larger function is I couldn’t get a for loop to iterate for some reason in this exercise. I ended up just using a while loop and got it working. Thanks for the direction.

Congratulations on working through a tough problem. I’m glad you were able to figure it out. I agree that a while loop is a cleaner way to solve this problem.

Happy coding!

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.