Split into chunks
A JavaScript exercise from Scriptdrill. Solve it with a code editor in your browser that runs your function against example and hidden tests, and it is checked the moment you submit.
The task
Split `arr` into arrays of `size` items each. The last chunk holds whatever is left, so it can be shorter. `size` is at least 1. Do not change `arr`.
What this exercise teaches
A loop with i += size visits the start of each chunk, and slice copies that piece, clipping at the end of the array. The number of chunks is the length divided by size, rounded up.
A hint if you get stuck
Step through the array `size` at a time and take `arr.slice(i, i + size)`. `slice` never changes the original.
This exercise is part of Pro
Pro unlocks all 45 exercises, with worked solutions and progress saved to your account. The free exercises work without an account.
You create a free account first, so Pro is attached to it.
Monthly is charged today and then every month until you cancel, in one click from your account page. Lifetime is one payment with no renewals. Full refund within 14 days of paying.