Sha256: 57485835d3f65c4386651799d652640f33a91a5e237f7d997044f546c183f563
Contents?: true
Size: 1.02 KB
Versions: 181
Compression:
Stored size: 1.02 KB
Contents
export default class Series { constructor(numberString) { if(numberString.match('[^0-9]')) throw new Error('Invalid input.'); this.numberString = numberString; this.digits = this.getDigits(); } getDigits() { return [...this.numberString].map( digit => { return parseInt(digit, 10); }); } largestProduct(size) { if (size < 0) throw new Error('Invalid input.'); let product, max = 0; this.slices(size).forEach( slice => { product = slice.reduce( (a, b) => { return a * b; }, 1); if (product > max) { max = product; } }); return max; } slices(sliceSize) { const result = []; let slice = []; if (sliceSize > this.digits.length) { throw new Error('Slice size is too big.'); } for (let i = 0; i < this.digits.length - sliceSize + 1; i++) { for (let j = 0; j < sliceSize; j++) { slice.push(this.digits[i + j]); } result.push(slice); slice = []; } return result; } }
Version data entries
181 entries across 181 versions & 1 rubygems