Implement the Chunky Monkey Algorithm - Implement the Chunky Monkey Algorithm

Tell us what’s happening:

I do not know if I am hallucinating, but this compiler does not correctly compile the code? I wrote code that was supposed to print out the empty new array, then push an element in the new array from the argument, then print out the result of the new array.
However, it prints out the newArray full of elements before the push method is even run?
I do not think it is supposed to push all elements in the first iteration? I entered the exact same code in other compilers and they worked normally. Below I have the difference between the freeCodeCamp console and the CodePen console with the same code inputted.

freeCodeCamp compiler:

Before push: 0 [ ‘a’, ‘b’, ‘c’, ‘d’ ] [ ‘a’, ‘b’, ‘c’, ‘d’ ]
After push: [ ‘a’, ‘b’, ‘c’, ‘d’ ] [ ‘a’, ‘b’, ‘c’, ‘d’ ]
Before push: 1 [ ‘a’, ‘b’, ‘c’, ‘d’ ] [ ‘a’, ‘b’, ‘c’, ‘d’ ]
After push: [ ‘a’, ‘b’, ‘c’, ‘d’ ] [ ‘a’, ‘b’, ‘c’, ‘d’ ]
Before push: 2 [ ‘a’, ‘b’, ‘c’, ‘d’ ] [ ‘a’, ‘b’, ‘c’, ‘d’ ]
After push: [ ‘a’, ‘b’, ‘c’, ‘d’ ] [ ‘a’, ‘b’, ‘c’, ‘d’ ]
Before push: 3 [ ‘a’, ‘b’, ‘c’, ‘d’ ] [ ‘a’, ‘b’, ‘c’, ‘d’ ]
After push: [ ‘a’, ‘b’, ‘c’, ‘d’ ] [ ‘a’, ‘b’, ‘c’, ‘d’ ]

CodePen Console:

"Before push: " 0 // [object Array] (4) [“a”,“b”,“c”,“d”] // [object Array] (0)

"After push: " // [object Array] (4) [“a”,“b”,“c”,“d”] // [object Array] (1) [“a”]

"Before push: " 1 // [object Array] (4) [“a”,“b”,“c”,“d”] // [object Array] (1) [“a”]

"After push: " // [object Array] (4) [“a”,“b”,“c”,“d”] // [object Array] (2) [“a”,“b”]

"Before push: " 2 // [object Array] (4) [“a”,“b”,“c”,“d”] // [object Array] (2) [“a”,“b”]

"After push: " // [object Array] (4) [“a”,“b”,“c”,“d”] // [object Array] (3) [“a”,“b”,“c”]

"Before push: " 3 // [object Array] (4) [“a”,“b”,“c”,“d”] // [object Array] (3) [“a”,“b”,“c”]

"After push: " // [object Array] (4) [“a”,“b”,“c”,“d”] // [object Array] (4) [“a”,“b”,“c”,“d”]

"Before push: " 0 // [object Array] (4) [“a”,“b”,“c”,“d”] // [object Array] (0)

"After push: " // [object Array] (4) [“a”,“b”,“c”,“d”] // [object Array] (1) [“a”]

"Before push: " 1 // [object Array] (4) [“a”,“b”,“c”,“d”] // [object Array] (1) [“a”]

"After push: " // [object Array] (4) [“a”,“b”,“c”,“d”] // [object Array] (2) [“a”,“b”]

"Before push: " 2 // [object Array] (4) [“a”,“b”,“c”,“d”] // [object Array] (2) [“a”,“b”]

"After push: " // [object Array] (4) [“a”,“b”,“c”,“d”] // [object Array] (3) [“a”,“b”,“c”]

"Before push: " 3 // [object Array] (4) [“a”,“b”,“c”,“d”] // [object Array] (3) [“a”,“b”,“c”]

"After push: " // [object Array] (4) [“a”,“b”,“c”,“d”] // [object Array] (4) [“a”,“b”,“c”,“d”]

If there is a bug in the compiler, please fix it. Thanks.

Your code so far

function chunkArrayInGroups(insertedArr, numOfArrays){
  let finalArr = [];
  let newArr = [];
  for(let i = 0; i<insertedArr.length; i++){
    console.log("Before push: ", i, insertedArr, newArr);
    newArr.push(insertedArr[i]);
    console.log("After push: ", insertedArr, newArr);
  }
}

chunkArrayInGroups(['a', 'b', 'c', 'd'], 2);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36

Challenge Information:

Implement the Chunky Monkey Algorithm - Implement the Chunky Monkey Algorithm

hello and welcome to fcc forum :slight_smile:

  • is there any real difference between these two?
  • its doing its job, unless there is something more to it, then feel free to explain a bit more, thanks :slight_smile:

happy coding :slight_smile:

you are changing newArr during the execution, when you print mutable objects it happens that console.log will print the last state instead of the state you think it should have. Try printing a copy of newArr, like newArr.slice()

anyway I have reported it here, it seems to be a new behaviour: