# brunnock's solution

## to Largest Series Product in the JavaScript Track

Published at Nov 24 2018 · 0 comments
Instructions
Test suite
Solution

#### Note:

This exercise has changed since this solution was written.

Given a string of digits, calculate the largest product for a contiguous substring of digits of length n.

For example, for the input `'1027839564'`, the largest product for a series of 3 digits is 270 (9 * 5 * 6), and the largest product for a series of 5 digits is 7560 (7 * 8 * 3 * 9 * 5).

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

For the input `'73167176531330624919225119674426574742355349194934'`, the largest product for a series of 6 digits is 23520.

## Setup

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

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

## Requirements

Install assignment dependencies:

``````\$ npm install
``````

## Making the test suite pass

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`.

## Source

A variation on Problem 8 at Project Euler http://projecteuler.net/problem=8

## Submitting Incomplete Solutions

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

### largest-series-product.spec.js

``````import { largestProduct } from './largest-series-product';

describe('Series', () => {
test('can get the largest product of 2', () => {
expect(largestProduct('0123456789', 2)).toBe(72);
});

xtest('works for a tiny number', () => {
expect(largestProduct('19', 2)).toBe(9);
});

xtest('can get the largest product of 3', () => {
expect(largestProduct('1027839564', 3)).toBe(270);
});

xtest('can get the largest product of a big number', () => {
const largeNumber = '73167176531330624919225119674426574742355349194934969835203127745063262395783180169848018694788'
+ '51843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648'
+ '95044524452316173185640309871112172238311362229893423380308135336276614282806444486645238749303589072962904915604'
+ '40772390713810515859307960866701724271218839987979087922749219016997208880937766572733300105336788122023542180975'
+ '12545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622'
+ '48283972241375657056057490261407972968652414535100474821663704844031998900088952434506585412275886668811642717147'
+ '99244429282308634656748139191231628245861786645835912456652947654568284891288314260769004224219022671055626321111'
+ '10937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636'
+ '899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450';
expect(largestProduct(largeNumber, 13)).toBe(23514624000);
});

xtest('returns 0 if all digits are zero', () => {
expect(largestProduct('0000', 2)).toBe(0);
});

xtest('returns 0 if all spans contain zero', () => {
expect(largestProduct('99099', 3)).toBe(0);
});

xtest('rejects invalid character in input', () => {
expect(() => {
largestProduct('1234a5', 2);
}).toThrow(new Error('Invalid input.'));
});

xtest('rejects negative span', () => {
expect(() => {
largestProduct('12345', -1);
}).toThrow(new Error('Invalid input.'));
});

xtest('returns 1 for empty string and zero slice length', () => {
expect(largestProduct('', 0)).toBe(1);
});

xtest('returns 1 for non-empty string and zero slice length', () => {
expect(largestProduct('123', 0)).toBe(1);
});

xtest('throws an error for slices bigger than the number', () => {
expect(() => {
largestProduct('123', 4);
}).toThrow(new Error('Slice size is too big.'));
});

xtest('throws an error for empty string and non-zero slice length', () => {
expect(() => {
largestProduct('', 1);
}).toThrow(new Error('Slice size is too big.'));
});
});``````
``````function largestProduct(string,limit) {
if ((isNaN(Number(string))) || (limit<0)) throw('Invalid input.');
if (limit > string.length) throw('Slice size is too big.');
if (limit===0) return 1;
const digits = string.split('').map(Number);
let greatest=0;
for (let index=0; index<=digits.length-limit; ++index) {
greatest = Math.max(greatest, digits.slice(index,index+limit).reduce( (factor,product) => factor*product));
}
return greatest;
}
module.exports={largestProduct};``````

Find this solution interesting? Ask the author a question to learn more.

### What can you learn from this solution?

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?