Chunky Monkey basic algorithm error

Tell us what’s happening:
I have been running this code on mycomputer and it works, however when it comes to here, it gives error, any idea?
Your code so far


function chunkArrayInGroups(arr, size) {
let newArr=[]
while(true){
  if(arr.length>=size){
    newArr.push([arr.splice(0,size)])
   
  }else if(arr.length>0&&arr.length<size){
   newArr.push([arr.splice(0)])
   
  }else{
    break;
  }
  console.log(arr.length)
  
 

 
}
return newArr
}

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

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

Challenge: Chunky Monkey

Link to the challenge:

splice returns an array so you simply need to push arr.splice(0,size) not [arr.splice(0,size)]

1 Like

thanks, it works, but i still don’t get why it works on my computer

There’s no difference between what the code does on your computer and what the code does in the FCC challenge editor. How do you test that “it works”?

(2) [Array(1), Array(1)]
0: Array(1)
0: Array(2)
0: "a"
1: "b"
length: 2
__proto__: Array(0)
length: 1
__proto__: Array(0)
1: [Array(2)]
length: 2
__proto__: Array(0)

I feel that you may be a bit confused as to what you are seeing in the browser console. When you call the function as you do in your example this is the outcome, and where the test wants an array that holds arrays that have strings in them which would look like [["a"],["b"]], what we have instead from your initial solution is an array that holds arrays which themselves hold an array which holds a string and that looks like [[["a"]],[["b"]]] which is not correct, but it is the expected outcome of the function you’ve written as you were pushing the return value of splice inside its own array, but splice already returns an array so you were always pushing a 2D array into a 1D array each time effectively making it a 3D array

1 Like