Created
September 26, 2018 19:59
-
-
Save jordanhudgens/662db5130d42a5189e4b6387f4ae1e56 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
class SubArraySplitter { | |
constructor(arr, num) { | |
this.arr = arr; | |
this.num = num; | |
this.splitArray = []; | |
this.iterate(); | |
} | |
iterate() { | |
while (this.arr.length > 1) { | |
this.segment(); | |
} | |
if (this.arr.length === 1) { | |
this.splitArray.push(...this.arr); | |
} | |
} | |
segment() { | |
this.splitArray.push(this.arr.slice(0, this.num)); | |
this.arr = this.arr.slice(this.num, this.arr.length); | |
} | |
} | |
const arr = new SubArraySplitter([1, 2, 3, 4], 2); //? | |
arr.splitArray; //? | |
const arrTwo = new SubArraySplitter([1, 2, 3, 4, 5], 2); //? | |
arrTwo.splitArray; //? | |
const arrThree = new SubArraySplitter([1, 2, 3, 4, 5, 6], 2); //? | |
arrThree.splitArray; //? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment