0

Working on a few beginner algorithmic exercises in JS. I am not understanding how "chunking" works when it gets to the "if" statement.

The main confusion I have is after iterating to the 2nd value; because in the first run, it meets the criteria of last being undefined; so the "1" pushes into the chunked variable. So once the second variable comes in, 2, since last is now defined and the length of Last does not equal to the "len" argument, it'll go to the else part. The 2 will get pushed into "last" and it'll form last = "[1],2"?

Then, when the third value (3), starts coming in, this is when I get even more confused. How does the if statement's chunked.push([number]) know to exactly push [1],2 into chunked when number at that point is also 3? Is it omitting 3?. I know the length of last at that point meets len but how does it formulate from [1],2 to the chunk [1,2]?

I was assuming 3 would also get pushed into "last".

I do apologize if this sounds confusing as well! I looked at many tutorials on solving this same problem online but no where explains it in detail.

function chunkedArr(arr, len){
    const chunked = [];

    for (let number of arr){
        const last = chunked[chunked.length-1] 

        console.log(last)

        if(!last || last.length === len){
            chunked.push( [number]);
        } else {
            last.push(number)
        }
    }

    return chunked;

}

console.log(chunkedArr([1,2,3,4,5,6],2))

2 Answers 2

1

One key insight could be that chunked is an array of arrays.

In the example, at the second iteration, chunked will not be [1], but [[1]]. Notice that it is an array of an array. So then last gets assigned the only sub-array inside chunked, i.e. [1], and in the else block, value 2 gets appended to that sub-array, so it becomes [1, 2]. Because last really is the sub-array that sits inside chunked, chunked now looks like this: [[1, 2]].

In the next iteration, the value is 3, and the if condition is true because now last is [1, 2], and thus having the required length 2. At this point the value of last is left untouched. This sub-array is now "full". A new subarray is appended to chunked, so that it looks like [[1, 2], [3]]

At the next iteration, the value is 4, and now last will be assigned the newer sub-array, i.e. [3]. And so it continues...

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! i never would have thought that pushing the number into "last" would also adjust the "chunked" variable. I see what you mean about last being [1] but I would have never figured it out on my own that if I were to push a number into last to make it [1,2], "chunked" would also mirror that.. learning something new everyday
haha I just marked your answer as well! Both are useful - hopefully u both get the credit. Still semi-new to this site so not sure how the crediting works. But you seem to have a lot! Amazing help.
0

Let's walk through your algorithm. Chunked array is called with 2 arguments: [1,2,3,4,5,6] and the len is 2 which is the desired size of the chunks. Inside your function, an empty array chunked is defined. Now the function iterates over the array arr.

  1. First iteration; last is undefined since, chunked is empty. The !last condition is true and therefore, [1] is pushed inside chunked. Now chunked has the following shape: [ [1] ].
  2. Now last is an array of length 1 with the following shape: [1]. !last is false and last.length === len is also false since len is 2. So, 2 is pushed into last. The shape of last is [1, 2] and the shape of chunked is [ [1, 2] ].
  3. Last is now an array of size 2 and shape [1, 2,]. The condition in if evaluates to true since last.length === len. So, an array with the current value, 3, is pushed into chunked. The size of chunked is now 2 and shape is [ [1, 2], [3] ].
  4. Last is defined and is an array of size 1 and shape [3]. If evaluate to false and 4 is pushed into last. Last has now size 2 and shape [3, 4]. Chunked array has size 2 and shape [ [1, 2], [3, 4] ].
  5. Now the current value is 5 and last is array of size 2. If condition is true and there, an array with value 5 is pushed into chunked. Chunked has size 3 and shape [ [1, 2], [3, 4], [5] ].
  6. Last is an array of size 1 and has value 6 in it. If is false and so, 6 is pushed to last. Last has size 2 and shape [5, 6] and chunked has shape [ [1, 2], [3,4], [5,6] ]

At this point the iteration over array terminates since there no more entries and the function returns chunked which when printed, has the same shape as chunked as in step 6. The function simply walks through the input array and copies over to a new array with the condition that the new array has entries which are all array of size len with values from arr taken consecutively.

1 Comment

Thank you so much for the details! Super deep;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.