Basic Algorithm Scripting - Chunky Monkey

Tell us what’s happening:

I can’t loop thru arr and slice it. The best I was able to do was push arr elements to new array i created. Can you tell me what I’m doing wrong?

Your code so far

function chunkArrayInGroups(arr, size) {
  let newArr = [];
  for (let i = 0; i < arr.length; i++) {
    return newArr += arr[i].slice();
  }
  return newArr;
}

console.log(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/124.0.0.0 Safari/537.36

Challenge Information:

Basic Algorithm Scripting - Chunky Monkey

1 Like

Hi @dustblackrose

Your code needs to output an array in chunks of two.
Try using the size parameter.

I don’t think you should return the array in the loop, otherwise it will output the first iteration, and stop the execution of the rest of the function.

Happy coding

Hello,

There isn’t just one solution to this problem; it’s a good one. Your approach isn’t entirely wrong, but you’re trying to slice each [i] indexed element one by one. Additionally, you’re returning just first element of array because using return. You don’t need to use return if you don’t want to stop the iteration.

You should chunk your elements into arrays of length defined by the size variable and return each chunk as a subarray in the resulting array.

One tip: you can push each chunk into your returning array, and they will be stored as arrays inside the array. Like Inception :rofl:.

function chunkArrayInGroups(arr, size) {
  let firstArray = [];
  let newArr = [];
  let secondArray = [];
  for (let i = 0; i < arr.length; i++) {
firstArray.push(arr[i]);
  }
  newArr.push(firstArray.slice(0, 2));
  secondArray = firstArray.slice(2, 4);
  newArr.push(secondArray)
  return newArr;
}

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

My new code. Can you please check it?

Where is 2 and 4 coming from?

Start and end of what I want it to slice

But is every single test case going to be that size?

No. That’s why I’m unsure what to do

Hmm, so maybe we should talk about what the target goal is.

Can you describe in your own words what should be the output of this function?

The output should be two dimensional arrays that divide items of arrays by the num

Ok, so instead of 2 you probably should be slicing using num somehow.

Alright, here’s another tip. First off, make sure you understand the logic behind your code. One of the most common pitfalls I’ve seen is folks not fully grasping the meaning behind the code they’ve written themselves. Try printing firstArray after the loop, then newArr after the push. Finally, print secondArray and firstArray before the function end.

So, you’ve got the chunkArrayInGroups(["a", "b", "c", "d"], 2) problem sorted, but what about the others? Consider how you’ve used the number 2. What exactly does 2 stand for? Why did you opt for 2 and 4? What’s the significance of this range? How can you tweak it to satisfy all scenarios? Right now, it seems like only one or two tests are happy.

And here’s one last tip: while using a for loop to cut chunks is one way to tackle it, make sure you set up the loop to keep everyone happy. It might be a unconventional, maybe a little out of the box…

1 Like

I don’t think it will let me tho. But I’ll try

Thank you

I think I need to arrays to store each chunk. And then I can push each array into newArr. That worked one time. What do you think?

That could work. I’m not sure exactly what you’re picturing but it seems like a step in the right direction.

Do think it’s necessary to iterate tho the array? I think I can push push items into new array. Then slice this array to have size elements in each. And then I push each array into newArr .

I think another loop would really help a lot, yeah

I think another loop would be unneccessary. One loop enough.

Enough talk, let’s see some action then.

I want to show you the concept with a similar problem, but it’s a little bit different. Here is the original problem if you’re interested. Also, I’ll show you solution in TypeScript. You should push yourself a bit to understand because the easy way is not the best way to learn. :grin:

Problem says:
Two children, Lily and Ron, want to share a chocolate bar. Each of the squares has an integer on it.

Lily decides to share a contiguous segment of the bar selected such that:

The length of the segment matches Ron's birth month, and,
The sum of the integers on the squares is equal to his birth day.

Determine how many ways she can divide the chocolate.

Example
s = [2, 2, 1, 3, 2]
d = 4
m = 2

Lily wants to find segments summing to Ron’s birth day, d = 4 with a length equalling his birth month, m=2. In this case, there are two segments meeting her criteria: [2, 2] and [1,3].

There is different way to solve this problem but lets make it similar with Chunky Monkey;

If you try to solve it with the same codes, it will be wrong again because there are tiny differences between these two problems. I believe you will find and solve it. The main aim of this ‘Chunky Monkey’ challenge is to improve problem-solving skills and to use your tools in different ways.

Happy coding!

It depends. Currently the first loop is copying the array. I’m trying to work with their current strategy and them them to throw it away :slight_smile:

Note that writing codes for people generally isn’t something we do here.

1 Like