Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/arrays.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function sum(arr) {
/** Splits an array into chunks of the given size. */
function chunk(arr, size) {
const result = [];
for (let i = 0; i <= arr.length; i += size) {
for (let i = 0; i < arr.length; i += size) {
result.push(arr.slice(i, i + size));
}
return result;
Expand Down
4 changes: 4 additions & 0 deletions tests/arrays.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,7 @@ test('sum returns 0 for an empty array', () => {
test('chunk splits an array into groups of the given size', () => {
assert.deepEqual(chunk([1, 2, 3, 4, 5], 2), [[1, 2], [3, 4], [5]]);
});

test('chunk does not append an empty group for evenly divisible arrays', () => {
assert.deepEqual(chunk([1, 2, 3, 4], 2), [[1, 2], [3, 4]]);
});