Chunky Monkey, where is the difference!?

hi campers

I’ve been a long time fighting to solve this challenge I’m trying every piece of code coming to my mind until I was really near to solve it then I’ve looked for people that solve that challenge before, anyway what I want to ask you about, is just why these pieces of code are the same but the first one doesn’t work rather than the second, where do you think the problem is?

the first one which doesn’t work:


function chunkArrayInGroups(arr, size) {
  // Break it up.
  let newArr = [];
  for(let i = 0; i < arr.length / size; i++){
  let r = arr.splice(0, size);
  newArr.push(r);
  }
  return newArr;
}

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

the second one which passes all the tests:


function chunkArrayInGroups(arr, size) {
  // Break it up.
  let newArr = [];
  let len = arr.length / size;
  for(let i = 0; i < len; i++){
  let r = arr.splice(0, size);
  newArr.push(r);
  }
  return newArr;
}

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

they seem similar but why the first one doesn’t even if they’re the same logicaly.

Aah okay

Thank you so much, guys, :slight_smile: :blush: what I understand is the array (arr) become short after the mutation pf splice() method and the value or arr.length / size become less by one after each iteration

is that exactly what I need to understand?