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