Skip to content

Instantly share code, notes, and snippets.

@preston-56
Created April 4, 2024 17:12
Show Gist options
  • Select an option

  • Save preston-56/7636920c2b54935dd90945a083542fe2 to your computer and use it in GitHub Desktop.

Select an option

Save preston-56/7636920c2b54935dd90945a083542fe2 to your computer and use it in GitHub Desktop.
// ([1,2,3,4,5,6,7],3) === [[1,2,3],[3,4,5],[6,7]]
// ([1,2,3,5,6,7],1) == [[1],[2],[3],[5],[6],[7]]
function splitArray(lst, n) {
let result = [];
for (let i = 0; i < lst.length; i += n) {
result.push(lst.slice(i, i + n));
}
return result;
}
// Test cases
console.log(splitArray([1,2,3,4,5,6,7],3));
console.log(splitArray([1,2,3,5,6,7],1));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment