Basic Algorithm Scripting: Chunky Monkey (Console.log shows test is passed but FCC won't accept it)

Basic Algorithm Scripting: Chunky Monkey (Console.log shows test is passed but FCC won't accept it)
0

#1

Can someone give me a hand with what the issue is? I’ve spent like two hours already…
It’s exactly with the input in this example that I’m getting the issue.

var result = [];
function chunkArrayInGroups(arr, size) {
  if(size < arr.length){
    let siRe = (arr.length%size!=0 ? 1 + parseInt(arr.length/size)                              :arr.length/size);
    console.log('Numbers of arrays within the result: '+siRe);
    for (let i = 0; i < siRe; i++){
      if(arr[i] !== ''){
        result[i] = (arr.splice(0,size));
        console.log('Array #'+ (i + 1) + ': '+ result[i]);
      }
      else{
        break;
      }
    }
  }
  else{
    return arr;
  }
  return result;
}

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

#2

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

Also, in the future when you have a question about a specific challenge as it relates to your written code for that challenge, just click the Ask for Help button located on the challenge. It will create a new topic with all code you have written and include a link to the challenge also. You will still be able to ask any questions in the post before submitting it to the forum.

Thank you.


#3

Very clear response. Thanks a lot!