| 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 | // Validate that all characters are digits |
| 17 | if (!/^[0-9]+$/.test(numberString)) { |
| 18 | throw new Error('series must contain only digits'); |
| 19 | } |
| 20 | |
| 21 | this.digits = numberString.split('').map(Number); |
| 22 | this.series = numberString; |
| 12 | 23 | } |
| 13 | 24 | |
| 14 | 25 | /** |
| 15 | | * @param {number} sliceSize |
| 16 | | * @returns {number[][]} |
| 26 | * Returns all contiguous substrings of the specified length from the series |
| 27 | * @param {number} sliceSize - The length of each substring to extract |
| 28 | * @returns {number[][]} - Array of arrays, each containing digits of a substring |
| 29 | * @throws {Error} - If sliceSize is invalid (negative, zero, or greater than series length) |
| 17 | 30 | */ |
| 18 | 31 | slices(sliceSize) { |
| 19 | | throw new Error('Remove this line and implement the function'); |
| 32 | // Edge Case: Check if slice length is negative |
| 33 | if (sliceSize < 0) { |
| 34 | throw new Error('slice length cannot be negative'); |
| 35 | } |
| 36 | |
| 37 | // Edge Case: Check if slice length is zero |
| 38 | if (sliceSize === 0) { |
| 39 | throw new Error('slice length cannot be zero'); |
| 40 | } |
| 41 | |
| 42 | // Edge Case: Check if slice length is greater than series length |
| 43 | if (sliceSize > this.digits.length) { |
| 44 | throw new Error('slice length cannot be greater than series length'); |
| 45 | } |
| 46 | |
| 47 | const result = []; |
| 48 | |
| 49 | // Edge Case: Handle when sliceSize equals the series length |
| 50 | if (sliceSize === this.digits.length) { |
| 51 | return [this.digits]; |
| 52 | } |
| 53 | |
| 54 | // Extract all contiguous substrings of the specified length |
| 55 | for (let i = 0; i <= this.digits.length - sliceSize; i++) { |
| 56 | result.push(this.digits.slice(i, i + sliceSize)); |
| 57 | } |
| 58 | |
| 59 | return result; |
| 20 | 60 | } |
| 21 | 61 | } |
| 62 | |
| 63 | // Handled Edge Cases: Empty series, non-digit characters in series, negative slice length, |
| 64 | // zero slice length, slice length greater than series length, slice length equals series length |