# Chunky Monkey - My take on it might be bananas

Disclaimer - Please, no one look at this as the way to solve this…Im 99% sure there’s a better way Im just putting this out there for prosperity.

Im just now learning JS, and I have been struggling like crazy through the exercises, but this one took me the longest of all of them, because I was determined to not look up any hints and Im so proud of myself for solving it that I could burst… However…I have this feeling in my gut that one day…I will look back at how I solved this, and hurt myself laughing.

I look forward to coming back to this one day and being able to see where I could have done better with it. But as for here and now…no one can say I didnt try my very best

1 Like

you’ve done great, whatever works, fuck performance when learning (pardon my french)

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

Are you kidding me? That lil line of code did it all…ooh boy. I knew I was overthinking things but…ohh boy!

So true though, at this stage Im just happy to be able to even figure this out. One week ago Id never written a line of JavaScript before, so Im happy with how far Ive come…You’re welcome to drop French in on me anytime…its such a beautiful language lol

3 Likes

lmao for not having ever written js before; you’re way above ‘the level’. and yes i love fuckin’ french

I did it a really weird way lol now that see @rudolphh’s solution ha!. I just ended up chopping hunks off the original and just starting from 0 to size each time.

``````function chunkArrayInGroups(arr, size) {
// Break it up.
var lengthNum = parseInt(arr.length/size);
var newArr = [];
for(let i = 0;i < lengthNum;i++)
{
newArr.push(arr.splice(0,size));
}
if(arr.length >= 1)
newArr.push(arr.splice(0,arr.length));

return newArr;
}``````

Yeah, this really makes you think in a completely different way…Really cool actually to see how tiny the actual answer is and yours is very small too! Right now Im happy to be able to figure out some kind of way to make it work, but knowing the possibilities, its going to be awesome when Ive learned enough to be able to look at it again and see a better way to do it.

@cndragn Think of solutions you first come up with like a first draft of a paper. When it comes to code, once you get it working, then you can look at pieces that can be grouped together and either put in another function, or ask “does the language (or some library, package, framework, etc.) have something that does this for me?” Almost any problem we face when learning has been done before, and someone has been kind enough to share, and even make it easier for us to reduce that cognitive load.

Like this array stuff, just take a look at the left-hand bar in the MDN documentation for arrays. read through each just to get a small idea of what these functions do. Sure you can use reduce and make a filter, map, etc., but there’s a reason why they were created. They, in a subjective/relative sense make your code more readable, are very common things we’ll do with these structures, and make it easier to work with them.

And if performance is really an issue, when in doubt for loop is likely king.

2 Likes

I approached the problem recursively: generate the first chunk and then, recursively, generate the rest, inserting the results into the answer array as we go:

``````function chunkArrayInGroups(arr, size) {
function helper(acc, myArr) {
if (myArr.length <= size) {
acc.push(myArr);
return acc;
} else {
acc.push(myArr.slice(0, size));
return helper(acc, myArr.slice(size, myArr.length));}
}
return helper([], arr);
}
``````

@rudolphh I really liked the use of the `for` loop–I thought it was a really concise and elegant solution.

Really good tip to work backwards… Just on a problem solving front, I start off that way, but…I often get close enough to at least clear some of the green checkmarks, and then i throw logic aside and start adding this and that and whatever to my code to fix whatever I need to get all the check marks down to green. Its usually not quite as bad as this one (I hope! lol Will definitely be interesting for me to go back through all my codes once I learn more) …this one just really struck me as…yeeeeah, I definitely took the scenic route here.

Def will try though to not focus so much on just clearing the green, and on seeing what Im doing wrong as I work through it.