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