Show me the monkey!

Tell us what’s happening:
can someone who knows what they’re doing tell me why i shouldn’t use this code?

after i pass the tests i always look at the other possible solutions to see how i could have coded it. i usually take the NASA route in programming… but my version looked like a mashup of several answers. any tips on how to improve this would be appreciated!

Your code so far

function chunkArrayInGroups(arr, size) {

let newChunk = [];

for (var i = 0; i < arr.length;newChunk.push(arr.splice(0,size))) {

} return newChunk;

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

Your browser information:

User Agent is: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:82.0) Gecko/20100101 Firefox/82.0.

Challenge: Chunky Monkey

Link to the challenge:

one reason is that you are changing the input element, meaning your function has side effects and it’s bad practice

you are also changing the array on which you are looping, meaning that you are having an unintended behaviour for the loop.


You have no code in the body of the loop. I’m not sure how you ended up with that code structure. You seem to have replaced the incrementing part of the loop code with the body of the loop.

1 Like

I think the variable i in your code is redundant, as it is always 0.
So, your loop is essentially equivalent to

while(arr.length > 0)

I think in this form the code would be less confusing. But, as @ilenia noted below, it’s usually not a good idea to mutate the input inside your function.

1 Like

thank you all! i think the code you supplied is what i intended to do. . .

am i just inviting bugs by mutating the input?