Files
wehub-resource-sync dde272c4b8
i18n - Build Validation / Validate i18n Builds (24) (push) Has been cancelled
CI - Node.js / Lint (24) (push) Has been cancelled
CI - Node.js / Build (24) (push) Has been cancelled
CI - Node.js / Test (24) (push) Has been cancelled
CI - Node.js / Test - Upcoming Changes (24) (push) Has been cancelled
CI - Node.js / Test - i18n (italian, 24) (push) Has been cancelled
CI - Node.js / Test - i18n (portuguese, 24) (push) Has been cancelled
CD - Docker - GHCR Images / Build and Push Images (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 11:55:53 +08:00

1.5 KiB

id, title, challengeType, dashedName
id title challengeType dashedName
6a2037a68a0bc2aef0006001 Challenge 339: Array Chunks 28 challenge-339

--description--

Given an array and a chunk size, return the array split into sub-arrays of that size.

  • The last chunk may be smaller if the array doesn't divide evenly.

--hints--

chunkArray([1, 2, 3, 4, 5, 6], 3) should return [[1, 2, 3], [4, 5, 6]].

assert.deepEqual(chunkArray([1, 2, 3, 4, 5, 6], 3), [[1, 2, 3], [4, 5, 6]]);

chunkArray([1, "two", 3, "four", 5, "six", 7, "eight"], 2) should return [[1, "two"], [3, "four"], [5, "six"], [7, "eight"]].

assert.deepEqual(chunkArray([1, "two", 3, "four", 5, "six", 7, "eight"], 2), [[1, "two"], [3, "four"], [5, "six"], [7, "eight"]]);

chunkArray([1, 2, 3, 4, 5], 3) should return [[1, 2, 3], [4, 5]].

assert.deepEqual(chunkArray([1, 2, 3, 4, 5], 3), [[1, 2, 3], [4, 5]]);

chunkArray(["a", "b", "c", "d", "e"], 1) should return [["a"], ["b"], ["c"], ["d"], ["e"]].

assert.deepEqual(chunkArray(["a", "b", "c", "d", "e"], 1), [["a"], ["b"], ["c"], ["d"], ["e"]]);

chunkArray([1, 2, 3], 5) should return [[1, 2, 3]].

assert.deepEqual(chunkArray([1, 2, 3], 5), [[1, 2, 3]]);

--seed--

--seed-contents--

function chunkArray(arr, size) {

  return arr;
}

--solutions--

function chunkArray(arr, size) {
  const result = [];
  for (let i = 0; i < arr.length; i += size) {
    result.push(arr.slice(i, i + size));
  }
  return result;
}