Chunky Monkey, where is the difference!?

Chunky Monkey, where is the difference!?
0

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.

They’re both very different.

.splice() mutates an array.

Thus your first example the loop starts off as i < 2 but then changes to i < 1.
Whereas your second example the loop stays at i < 2.

1 Like

In the first code, since you check if i < arr.length / size, on the start of the 3rd iteration, i would be 2 and arr.length /size would be 0.5, so it would not continue.

1 Like

Uhm… how does it get to 0.5? Should stop at 1 < 1.

1 Like

Maybe my thinking is off (I only got 3.5 hours sleep last night). but when I use the following to check the values:

function chunkArrayInGroups(arr, size) {
  // Break it up.
  let newArr = [];
  console.log('i starts as 0');
  console.log('arr.length /size starts as ' + arr.length / size);
  console.log()
  for (let i = 0; i < arr.length / size; i++) {
    let r = arr.splice(0, size);
    newArr.push(r);
    console.log('next i will be ' + (i + 1));
    console.log('next arr.length / size will be ' + arr.length / size);
    console.log()
  }
  return newArr;
}

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

I get the following in the console:

i starts as 0
arr.length /size starts as 2.5

next i will be 1
next arr.length / size will be 1.5

next i will be 2
next arr.length / size will be 0.5

The last values shown above are what will be used to evaluate the for loop condition, so there is no third execution of the inner for loop code block.

1 Like

Oh you added a 5th element.

2 Likes

Oh yes. Sorry for the confusion. When I first looked at the user’s code, I assumed they were getting the correct answer (did not test code) for even number of elements, but failing on odd number of elements, so I added the extra element to show why.

When I run the same code above for the original example of 4 elements, it does exactly what you stated.

i starts as 0
arr.length /size starts as 2

next i will be 1
next arr.length / size will be 1
1 Like

3.5hrs?! Pffft. Get some sleep!

1 Like

Was working on something for a client and was testing out something new I had not used before.

1 Like

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?

Well splice mutates your array.

So not only is your length getting smaller but your iterator is still increasing by 1 per iteration.

1 Like