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