Chunky monkey problemo

Tell us what’s happening:
Hey,
Through a loop, i have applied the .splice() on the original array into pieces and assigning them to the empty arr2.
When i add an else condition, the green checks shift (i.e., those that i got right become wrong, and those that were wrong become right).
Kindly help me understand where i am getting it wrong.

  **Your code so far**

function chunkArrayInGroups(arr, size) {
let arr2 = [];
  if(arr.length >= size) {
    for(let i = 0; i < arr.length + 1; i++) {
      console.log(arr);
      arr2[i] = arr.splice(0, size);
    } 
  } else {
    arr2.push(arr.splice(0));
  }
  return arr2;
}

console.log(chunkArrayInGroups([0, 1, 2, 3, 4, 5, 6, 7, 8], 2));
  **Your browser information:**

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

Challenge: Chunky Monkey

Link to the challenge:

1 Like

Hi, Wairua)

One of the things that helped me was to add “=” to this part:

(let i = 0; i <= arr.length + 1; i++)

Still, your code didn’t pass one of the tests when we have one number left. I see you tried to use else for this, but if you would run your code on Python Tutor, you would see your code passes the else statement and goes right into return part, forgetting about the last number.

I managed to pass all the tests with your code after deleting the else statement and adding another if statement to check if the arr argument is of length 1.

Cheers)

1 Like

thank you for the feedback.

Think about the logic you have here:

* if arr is longer than size,
  * loop over arr, incrementing an index based on the length of arr,
    * remove a part of arr and stick it on the end of a new array.
  * end the loop.
* if arr *is not* longer than size, copy into a new member of that new array.

Now, there are some interesting moving parts here, some of which are turning my brain inside out. For example, i is incrementing till it meets arr.length-1 … But arr.length is also a moving target, and i is being used in reference to arr2 while getting its reference from arr

It’s not necessarily wrong, but it’s kind of convoluted. You’re right, you need to loop over that arr and pull off bits, but consider this one:

* while there is anything in arr,
   * pull off a chunk of arr up to size in length, and append it as a nested array to arr2.

That takes care of the looping, and the if/else.

2 Likes

you’re a 'lifesaver! thank you. I was stuck here for over a day trying to understand what is wrong.
If I may ask, what circumstances warrant for the use of a while loop over a for-loop?

1 Like

A for loop is great when you’re iterating a known number of times. For example, if you were to calculate the maximum number of chunks in advance (say, round up arr.length divided by size), you might use a loop - as you have a known exit condition.

If, on the other hand, you want to loop until a given condition is met, while is your tool. For example, suppose you want to allow a user to type one of three words at a prompt. While user input is not one of those words, keep looping. How many times? As many as it takes until that exit condition, which can change inside the while is met.

Generally, a for loop is used to represent a known, finite series of iterations, but a while is an unknown, potentially endless loop. Games often use while loops, as it isn’t known how many rounds a player might last.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.