Chunky Monkey - Objectives *check* but won't pass

Tell us what’s happening:
I compared my results against the objectives and the results are as they should be but it won’t pass the challenge. I can’t see what I am doing wrong.

Your code so far

function chunkArrayInGroups(arr, size) {
  // Break it up.
  for (var i=0;i<arr.length;i+=size) {
    chunks.push(arr.slice(i, i+size));
  }
  return chunks;
}

chunkArrayInGroups([0, 1, 2, 3, 4, 5, 6, 7, 8], 2);

Your browser information:

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

Link to the challenge:
https://www.freecodecamp.org/challenges/chunky-monkey

chunks is not defined

I had “var chunks = [];” outside the function. Thought that would work?

Thanks.

Yes that would work but I believe that FCC tests require local rather than global variables (the reasons are explained in the forum somewhere but I can’t remember why).

(and if you are doing that you should include this fact in your code snippet).

1 Like

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 new 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
3 Likes