ðŸŽ‰ Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io ðŸŽ‰

Published at Aug 18 2019
·
0 comments

Instructions

Test suite

Solution

Given a string of digits, output all the contiguous substrings of length `n`

in
that string in the order that they appear.

For example, the string "49142" has the following 3-digit series:

- "491"
- "914"
- "142"

And the following 4-digit series:

- "4914"
- "9142"

And if you ask for a 6-digit series from a 5-digit string, you deserve whatever you get.

Note that these series are only required to occupy *adjacent positions*
in the input; the digits need not be *numerically consecutive*.

Go through the setup instructions for Javascript to install the necessary dependencies:

https://exercism.io/tracks/javascript/installation

Install assignment dependencies:

```
$ npm install
```

Execute the tests with:

```
$ npm test
```

In the test suites all tests but the first have been skipped.

Once you get a test passing, you can enable the next one by changing `xtest`

to
`test`

.

A subset of the Problem 8 at Project Euler http://projecteuler.net/problem=8

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

```
import { Series } from './series';
describe('Series', () => {
test('has digits (short)', () => {
expect(new Series('01234').digits).toEqual([0, 1, 2, 3, 4]);
});
xtest('has digits (long)', () => {
expect(new Series('0123456789').digits)
.toEqual([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
});
xtest('keeps the digit order if reversed', () => {
expect(new Series('9876543210').digits)
.toEqual([9, 8, 7, 6, 5, 4, 3, 2, 1, 0]);
});
xtest('keeps arbitrary digit order', () => {
expect(new Series('936923468').digits)
.toEqual([9, 3, 6, 9, 2, 3, 4, 6, 8]);
});
xtest('can slice by 1', () => {
expect(new Series('01234').slices(1))
.toEqual([[0], [1], [2], [3], [4]]);
});
xtest('can slice by 2', () => {
expect(new Series('98273463').slices(2))
.toEqual([[9, 8], [8, 2], [2, 7], [7, 3], [3, 4], [4, 6], [6, 3]]);
});
xtest('can slice by 3', () => {
expect(new Series('01234').slices(3))
.toEqual([[0, 1, 2], [1, 2, 3], [2, 3, 4]]);
});
xtest('can slice by 3 with duplicate digits', () => {
expect(new Series('31001').slices(3))
.toEqual([[3, 1, 0], [1, 0, 0], [0, 0, 1]]);
});
xtest('can slice by 4', () => {
expect(new Series('91274').slices(4))
.toEqual([[9, 1, 2, 7], [1, 2, 7, 4]]);
});
xtest('can slice by 5', () => {
expect(new Series('81228').slices(5))
.toEqual([[8, 1, 2, 2, 8]]);
});
xtest('throws an error if not enough digits to slice', () => {
expect(() => {
new Series('01032987583').slices(12);
}).toThrow(new Error('Slice size is too big.'));
});
});
```

```
export class Series {
constructor(numbers) {
this.series = numbers.split('').map(Number);
}
get digits() {
return this.series;
}
slices(number) {
let slices = [];
if (number > this.series.length) throw new Error('Slice size is too big.');
for (let i = 0; i < this.series.length; i++) {
let slice = this.series.slice(i, i + number);
if (slice.length === number) {
slices.push(slice);
}
}
return slices;
}
}
```

A huge amount can be learned from reading other peopleâ€™s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

- What compromises have been made?
- Are there new concepts here that you could read more about to improve your understanding?

Level up your programming skills with 3,428 exercises across 52 languages, and insightful discussion with our volunteer team of welcoming mentors.
Exercism is
**100% free forever**.

## Community comments