Created
March 15, 2021 07:35
-
-
Save jlaguilargomez/f63e401c24d8341452fcecf382017f93 to your computer and use it in GitHub Desktop.
Implement recursivity to create a group of elements with N length from an original Array
This file contains 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
const arr = [1, 2, 3, 4, 5, 6]; | |
let newArr = []; | |
const spliceArray = (originalArr, newArr, groupLength) => { | |
newArr.push(originalArr.splice(0, groupLength)); | |
return originalArr.length === 0 | |
? newArr | |
: spliceArray(originalArr, newArr, groupLength); | |
}; | |
newArr = spliceArray(arr, newArr, 1); | |
console.log(newArr); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment