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