Skip to content

Instantly share code, notes, and snippets.

@jordanhudgens
Created September 26, 2018 19:59
Show Gist options
  • Save jordanhudgens/662db5130d42a5189e4b6387f4ae1e56 to your computer and use it in GitHub Desktop.
Save jordanhudgens/662db5130d42a5189e4b6387f4ae1e56 to your computer and use it in GitHub Desktop.
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