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

Version Path
trackler-2.1.0.25 tracks/ecmascript/exercises/largest-series-product/example.js
trackler-2.1.0.24 tracks/ecmascript/exercises/largest-series-product/example.js
trackler-2.1.0.23 tracks/ecmascript/exercises/largest-series-product/example.js
trackler-2.1.0.22 tracks/ecmascript/exercises/largest-series-product/example.js
trackler-2.1.0.21 tracks/ecmascript/exercises/largest-series-product/example.js
trackler-2.1.0.20 tracks/ecmascript/exercises/largest-series-product/example.js
trackler-2.1.0.19 tracks/ecmascript/exercises/largest-series-product/example.js
trackler-2.1.0.18 tracks/ecmascript/exercises/largest-series-product/example.js
trackler-2.1.0.17 tracks/ecmascript/exercises/largest-series-product/example.js
trackler-2.1.0.16 tracks/ecmascript/exercises/largest-series-product/example.js
trackler-2.1.0.15 tracks/ecmascript/exercises/largest-series-product/example.js
trackler-2.1.0.14 tracks/ecmascript/exercises/largest-series-product/example.js
trackler-2.1.0.13 tracks/ecmascript/exercises/largest-series-product/example.js
trackler-2.1.0.12 tracks/ecmascript/exercises/largest-series-product/example.js
trackler-2.1.0.11 tracks/ecmascript/exercises/largest-series-product/example.js
trackler-2.1.0.10 tracks/ecmascript/exercises/largest-series-product/example.js
trackler-2.1.0.9 tracks/ecmascript/exercises/largest-series-product/example.js
trackler-2.1.0.8 tracks/ecmascript/exercises/largest-series-product/example.js
trackler-2.1.0.7 tracks/ecmascript/exercises/largest-series-product/example.js
trackler-2.1.0.6 tracks/ecmascript/exercises/largest-series-product/example.js