| 5 | 5 | |
| 6 | 6 | export class Series { |
| 7 | 7 | constructor(series) { |
| 8 | | throw new Error('Remove this line and implement the function'); |
| 8 | // Edge Case: empty input string |
| 9 | if (series === '') { |
| 10 | throw new Error('series cannot be empty'); |
| 11 | } |
| 12 | this.series = series; |
| 9 | 13 | } |
| 10 | 14 | |
| 11 | 15 | slices(sliceLength) { |
| 12 | | throw new Error('Remove this line and implement the function'); |
| 16 | // Edge Case: slice length is zero |
| 17 | if (sliceLength === 0) { |
| 18 | throw new Error('slice length cannot be zero'); |
| 19 | } |
| 20 | |
| 21 | // Edge Case: slice length is negative |
| 22 | if (sliceLength < 0) { |
| 23 | throw new Error('slice length cannot be negative'); |
| 24 | } |
| 25 | |
| 26 | // Edge Case: slice length greater than series length |
| 27 | if (sliceLength > this.series.length) { |
| 28 | throw new Error('slice length cannot be greater than series length'); |
| 29 | } |
| 30 | |
| 31 | const result = []; |
| 32 | |
| 33 | // Edge Case: single character input string with slice length of 1 |
| 34 | if (this.series.length === 1 && sliceLength === 1) { |
| 35 | result.push([parseInt(this.series[0])]); |
| 36 | return result; |
| 37 | } |
| 38 | |
| 39 | for (let i = 0; i <= this.series.length - sliceLength; i++) { |
| 40 | const slice = []; |
| 41 | for (let j = 0; j < sliceLength; j++) { |
| 42 | slice.push(parseInt(this.series[i + j])); |
| 43 | } |
| 44 | result.push(slice); |
| 45 | } |
| 46 | |
| 47 | return result; |
| 13 | 48 | } |
| 14 | 49 | } |
| 50 | |
| 51 | // Handled Edge Cases: empty input string, slice length is zero, slice length is negative, slice length greater than series length, single character input string with slice length of 1 |