Instructions
Given a string of digits, output all the contiguous substrings of length n in that string in the order that they appear.
Our Solution
export class Series {
constructor(series) {
if (!series) throw new Error('series cannot be empty');
this.series = series;
}
slices(sliceLength) {
if (sliceLength < 0) throw new Error('slice length cannot be negative');
if (sliceLength === 0) throw new Error('slice length cannot be zero');
if (sliceLength > this.series.length) throw new Error('slice length cannot be greater than series length');
return Array.from({ length: this.series.length - sliceLength + 1 }, (_, i) =>
this.series.slice(i, i + sliceLength)
);
}
}
Source: Exercism javascript/series