| 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 |
| 10 | * @throws {Error} If the input string is empty |
| 9 | 11 | */ |
| 10 | 12 | constructor(numberString) { |
| 11 | | throw new Error('Remove this line and implement the function'); |
| 13 | // Edge Case: Empty series input |
| 14 | if (!numberString) { |
| 15 | throw new Error('series cannot be empty'); |
| 16 | } |
| 17 | |
| 18 | this.digits = numberString.split('').map(Number); |
| 19 | |
| 20 | // Edge Case: String contains non-digit characters (implicit validation) |
| 21 | // This is handled by the fact that non-digits will become NaN when converted to Number |
| 22 | if (this.digits.some(isNaN)) { |
| 23 | throw new Error('series cannot be empty'); // Using the same error as empty for invalid input |
| 24 | } |
| 25 | |
| 26 | this.series = numberString; |
| 27 | |
| 28 | // Handled Edge Cases: Empty series input, Non-digit characters in input |
| 12 | 29 | } |
| 13 | 30 | |
| 14 | 31 | /** |
| 15 | | * @param {number} sliceSize |
| 16 | | * @returns {number[][]} |
| 32 | * Returns all contiguous substrings of the specified length from the series |
| 33 | * @param {number} sliceSize - The length of each substring to extract |
| 34 | * @returns {number[][]} An array of arrays, each containing the digits of a substring |
| 35 | * @throws {Error} If sliceSize is invalid (negative, zero, or greater than series length) |
| 17 | 36 | */ |
| 18 | 37 | slices(sliceSize) { |
| 19 | | throw new Error('Remove this line and implement the function'); |
| 38 | // Edge Case: Negative slice length |
| 39 | if (sliceSize < 0) { |
| 40 | throw new Error('slice length cannot be negative'); |
| 41 | } |
| 42 | |
| 43 | // Edge Case: Zero slice length |
| 44 | if (sliceSize === 0) { |
| 45 | throw new Error('slice length cannot be zero'); |
| 46 | } |
| 47 | |
| 48 | // Edge Case: Slice length greater than series length |
| 49 | if (sliceSize > this.series.length) { |
| 50 | throw new Error('slice length cannot be greater than series length'); |
| 51 | } |
| 52 | |
| 53 | const result = []; |
| 54 | |
| 55 | // Edge Case: Slice length equal to series length (only one slice possible) |
| 56 | if (sliceSize === this.series.length) { |
| 57 | result.push([...this.digits]); |
| 58 | return result; |
| 59 | } |
| 60 | |
| 61 | // Extract all contiguous substrings of the specified length |
| 62 | for (let i = 0; i <= this.digits.length - sliceSize; i++) { |
| 63 | result.push(this.digits.slice(i, i + sliceSize)); |
| 64 | } |
| 65 | |
| 66 | return result; |
| 67 | |
| 68 | // Handled Edge Cases: Negative slice length, Zero slice length, Slice length greater than series length, Slice length equal to series length |
| 20 | 69 | } |
| 21 | 70 | } |