Sha256: c353abe571a5b7492c058d4e218453c07f39d5d961e6f2ad1ab8363db02aeeb6
Contents?: true
Size: 1.06 KB
Versions: 131
Compression:
Stored size: 1.06 KB
Contents
export default class Series { private numberString: string private digits: number[] constructor(numberString: string) { if (numberString.match('[^0-9]')) { throw new Error('Invalid input.') } this.numberString = numberString this.digits = this.getDigits() } getDigits() { return [...this.numberString].map((digit) => parseInt(digit, 10)) } largestProduct(size: number) { if (size < 0) { throw new Error('Invalid input.') } let product, max = 0 this.slices(size).forEach((slice) => { product = slice.reduce((a, b) => a * b, 1) if (product > max) { max = product } }) return max } slices(sliceSize: number) { 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
131 entries across 131 versions & 1 rubygems