Basic Algorithm Scripting - Chunky Monkey

Tell us what’s happening:
Hello all! I have passed most test cases for Chunky Monkey but I am stumped when it comes to adding additional arrays to meet the conditions for the algorithm. My issue is that the if statement that I created will only execute once before ending and only one additional array is created :frowning:. What I’m thinking is that if I could create a new array within the for loop that detected whenever the former arrays length matches the size variable although I could be on an entirely wrong path. Thanks for reading!

Your code so far

function chunkArrayInGroups(arr, size) {
  // push and slice will be used to split the array

  let newArr = [];

  for (let i = 0; i < arr.length; i++) {
    newArr.push(arr.splice(size));

    if (newArr.length === size) {
      let additionalArr = [];
      newArr.push(additionalArr);
      additionalArr.push(arr.splice(size));
    }
    
  }
  
  newArr.unshift(arr);
    console.log(newArr)
  return newArr;
  

  

}


chunkArrayInGroups([0, 1, 2, 3, 4, 5], 2);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/110.0

Challenge: Basic Algorithm Scripting - Chunky Monkey

Link to the challenge:

Are you sure you want to use just one argument to splice?

MDN: Array.prototype.splice

Also, your for loop is not quite working properly if you are going to splice the array passed into the function. Remember, when you splice an array you actually reduce the size of the array.

I think the better way to do this would be to extract elements from the array without changing it. Do you know of another array method that sounds similar to splice but doesn’t alter the original array?

I took a short lunch break and ready to give it another go :). Would I slice the elements until it has the amount that matches the size? I think where I got a little confused was how to achieve the multidimensional array that’s required :(. Definitely tricky haha. In my head, whenever I get to a certain point, I’ll have created multiple arrays explicitly and add them but that definitely isn’t the way to do it, at least I don’t think it is. Thank you so much for helping me also!

I like the sound of slice. I think you are on the right track there.

Don’t get me wrong, it can be done with splice. But generally you try to avoid changing the inputs to functions directly if they are passed by reference.

Both the slice and splice methods return an array, so you just merely push that returned array to an array, as you were doing. So I think you have that part down. Your issue was really with the repercussions of using splice. Perhaps using slice will make a little more sense? And like I said, it’s probably a best practice to use slice here as well.

Thanks so much! I just learned something new! Now that I think about it, it actually makes sense to not change an input passed by reference. I’m about to have another go at it, thank you for setting me on the right path!

Hey there again! I have a problem and wonder if you could point me in the right direction again! So with your help, I was able to get my code to output multiple arrays but they aren’t lining up as they are supposed to. I know my math is probably a little off but I was wondering if I’m even on the right path? Heres the code

function chunkArrayInGroups(arr, size) {
  // push and slice will be used to split the array

  let newArr = [];
  let slicedArr = [];
 
  for (let i = 0; i < arr.length - size; i++) {
    slicedArr.push(arr.slice(i, i + size));
    console.log(slicedArr)
    if (slicedArr.length === size) {
      newArr.push(slicedArr);
    }
  }
  console.log(newArr)

}

Console log outputs this

[ [ 0, 1 ] ]
[ [ 0, 1 ], [ 1, 2 ] ]
[ [ 0, 1 ], [ 1, 2 ], [ 2, 3 ] ]
[ [ 0, 1 ], [ 1, 2 ], [ 2, 3 ], [ 3, 4 ] ]
[ [ [ 0, 1 ], [ 1, 2 ], [ 2, 3 ], [ 3, 4 ] ] ]

sorry if I am breaking forum formatting rules :frowning: I am terrible at forum posting but would like to get better. Thanks so much!

To display your code in here you need to wrap it in triple back ticks. On a line by itself type three back ticks. Then on the first line below the three back ticks paste in your code. Then below your code on a new line type three more back ticks. The back tick on my keyboard is in the upper left just above the Tab key and below the Esc key. You may also be able to use Ctrl+e to automatically give you the triple back ticks while you are typing in the this editor and the cursor is on a line by itself. Alternatively, with the cursor on a line by itself, you can use the </> button above the editor to add the triple back ticks.

What is the purpose of this in the for loop?

Think very carefully about i. Do you really want to loop through the array one element at a time? For example, if you want to extract two elements at a time, the first time through the loop you would want to start at 0. But after extracting the first two elements, where would you want to start for the second loop?

I think I understand why you are doing this, but you don’t need to do this. Even though it is not stated in the instructions, I believe the tests will always provide the proper amount of elements in the array to be split evenly by size.

Sorry, the arr.length - size thing was a mistake that I forgot to correct before copying the code, it was originally i < arr.length; Okay I just repeated your response three times in my head and I think I get where you’re going at. I think I have to increment i by size’s amount. Going to test it right now…even if it isn’t size’s amount though, I definitely feel like I’m getting very close. Thanks again!

I wrote up an entire post thanking you for your help but I formatted it wrong and the response ended up in the block of code haha but I got it now. I wanted to thank you for your help yesterday in setting me on the right path. I couldn’t solve it last night unfortunately, but when I woke up this morning, I was able to solve it within 10 minutes. I really appreciate that you helped me within my range of knowledge and didn’t just give me the answer. Thanks so much!

unction chunkArrayInGroups(arr, size) {
  // push and slice will be used to split the array

  let newArr = [];
   

  for (let i = 0; i < arr.length; i += size) {
    newArr.push(arr.slice(i, i + size))
    

  }
  
  console.log(newArr)
  return newArr;

  

}


chunkArrayInGroups([0, 1, 2, 3, 4, 5], 2);
1 Like

Awesome. Sometimes the best thing you can do is walk away from it for a while and let your brain refresh. Or take a shower :slightly_smiling_face:

You’re welcome. That’s the best compliment you can get around here. The goal is to help you discover the answer for yourself.

Keep up the good work!

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