| 8 | 8 | * @param {string} numberString |
| 9 | 9 | */ |
| 10 | 10 | constructor(numberString) { |
| 11 | throw new Error('Remove this line and implement the function'); | |
| 11 | // Edge Case: Series cannot be empty | |
| 12 | if (numberString === '') { | |
| 13 | throw new Error('series cannot be empty'); | |
| 14 | } | |
| 15 | ||
| 16 | this.digits = numberString.split('').map(Number); | |
| 17 | this.series = numberString; | |
| 12 | 18 | } |
| 13 | 19 | |
| 14 | 20 | /** |
| 16 | 22 | * @returns {number[][]} |
| 17 | 23 | */ |
| 18 | 24 | slices(sliceSize) { |
| 19 | throw new Error('Remove this line and implement the function'); | |
| 25 | // Edge Case: Slice length cannot be negative | |
| 26 | if (sliceSize < 0) { | |
| 27 | throw new Error('slice length cannot be negative'); | |
| 28 | } | |
| 29 | ||
| 30 | // Edge Case: Slice length cannot be zero | |
| 31 | if (sliceSize === 0) { | |
| 32 | throw new Error('slice length cannot be zero'); | |
| 33 | } | |
| 34 | ||
| 35 | // Edge Case: Slice length cannot be greater than series length | |
| 36 | if (sliceSize > this.series.length) { | |
| 37 | throw new Error('slice length cannot be greater than series length'); | |
| 38 | } | |
| 39 | ||
| 40 | const result = []; | |
| 41 | ||
| 42 | // Edge Case: Handle when sliceSize equals the series length | |
| 43 | if (sliceSize === this.series.length) { | |
| 44 | result.push(this.digits); | |
| 45 | return result; | |
| 46 | } | |
| 47 | ||
| 48 | // Sliding window approach to extract all contiguous substrings | |
| 49 | for (let i = 0; i <= this.digits.length - sliceSize; i++) { | |
| 50 | result.push(this.digits.slice(i, i + sliceSize)); | |
| 51 | } | |
| 52 | ||
| 53 | return result; | |
| 20 | 54 | } |
| 21 | 55 | } |
| 56 | ||
| 57 | // Handled Edge Cases: Series cannot be empty, slice length cannot be negative, slice length cannot be zero, slice length cannot be greater than series length, handle when sliceSize equals the series length |