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