Javascript Algorithms

Javascript Algorithms
0.0 0

#1

So I have a weird question to ask. I started work on solving the Chunky Monkey algorithm. I spent a couple days trying to figure it out, Googling some ideas, and came up with this code here:

function chunkArrayInGroups(arr, size) {
 var newArr = [];
 var test = arr.length;


 for (var i = 0; i < test; i = i + size) {
	
	var arr2 = arr.splice(0, size);
	newArr.push(arr2);	
}

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

Works perfectly, but my problem is, I don’t quite understand how my own code works in spots. :stuck_out_tongue:

So I’ll outline the areas I’m confused on.

  1. I tried using my for-loop to run the length of the array, but it doesn’t loop through all the numbers in the array and push them in my new one. Placing the length in a variable and looping through that works.

  2. Weird part. Inside the for-loop, I add another variable to store my arr parameter with the splice method added. It didn’t occur to me until later that I stored arr in variable arr2. I would have thought I should have stored test instead, since I’m looping through that. But when I altered the code, I got the wrong results.

If anyone could kindly just break things down and explain these two points to me, it would be really appreciated.


#2

I’ve edited your post for readability. When you enter a code block into the forum, precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.


#3

As a general rule you shouldn’t modify something you’re looping through

If you loop over the indices of array arr and then modify arr so it has less indices, you’re looking for trouble. This is for several reasons, but I’ll explain why it wasn’t working for this individual case first.

The test part of a for loop runs at the end of the loop body, and as splice has removed elements, arr.length has changed, so each time it’s called i is compared to a smaller number, smaller by size each time in your case.

Thus, the loop body executes less times that you wanted.


Modifying some kind of object as you loop through it can be much worse than this nice case though, consider a forEach on an array where the array is modified in some way during the process. How many times does the loop body execute? What actually happens? These things are at best harder to reason about and at worse causes catastrophically different behaviour from what you might expect

Realistically you should only ever do this if (a) you are 100% convinced you understand the control flow, and (b) understand the behaviour of doing so in the implementation of the programming language you’re using (it may even be undefined behaviour, depending on the language, this can be very very bad),

I’d even go as far as saying © there’s no better alternative - Other people may need to reason about the code too!