Basic Algorithm Scripting: Chunky Monkey: Recursive Solution not passing any tests

Tell us what’s happening:

So I came up with a recursive solution by calling the chunkArrayInGroups functions on the spliced array and declaring a global array outside the function. When I console.log(nestedArr); it prints the correct solution, but when it gets to the return statement, it results in “undefined.” What is going on?

  **Your code so far**

var nestedArr = [];
function chunkArrayInGroups(arr, size) {
if (arr.length <= size) {
  nestedArr.push(arr);
  return nestedArr;
}
if (arr.length > size) {
  var splicedArr = arr.splice(size);
  nestedArr.push(arr);
  chunkArrayInGroups(splicedArr, size);
}
}

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

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

Challenge: Chunky Monkey

Link to the challenge:

This is your issue. Using global variables is a sort of ‘faux recursion’ that breaks function reusability.


Your code contains global variables that are changed each time the function is run. This means that after each test completes, subsequent tests start with the previous value. To fix this, make sure your function doesn’t change any global variables, and declare/assign variables within the function if they need to be changed.

Example:

var myGlobal = [1];
function returnGlobal(arg) {
  myGlobal.push(arg);
  return myGlobal;
} // unreliable - array gets longer each time the function is run

function returnLocal(arg) {
  var myLocal = [1];
  myLocal.push(arg);
  return myLocal;
} // reliable - always returns an array of length 2

Thanks for your reply. Initially, I had set up var nestedArr = []; inside the function, but due to recursion, the array was being initialized to an empty array after each recursive call. Then nestedArr would only contain the last group of elements. And yet, the return statement return nestedArr resolves to undefined.

function chunkArrayInGroups(arr, size) {
  var nestedArr = [];
  if (arr.length <= size) {
    nestedArr.push(arr);
    console.log(nestedArr); // This prints out the array with only the last group of elements
    return nestedArr;// Once it gets to the return statement, I get "undefined"
  }
  if (arr.length > size) {
    var splicedArr = arr.splice(size);
    nestedArr.push(arr);
    chunkArrayInGroups(splicedArr, size);
  }
}

You need to use the return value of the recursive function call. There is no return value in your second if clause (by the way, it would be more conventional to make that an ‘else’)

Ahh, I see. I had just assumed the return value carried over! I added a return statement and edited the second if to an else clause. The function returns the correct array, but I don’t think the test likes that I am using a global variable, so now I’m trying to figure out how to implement the variable in the function or… just find a new way to solve this problem haha

1 Like

Yeah, the tests run multiple cases back to back, which means your global is never ‘cleared out’ and will contain more and more stuff from previous test cases.