Created
April 4, 2024 17:12
-
-
Save preston-56/7636920c2b54935dd90945a083542fe2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ([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