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.5 tracks/ecmascript/exercises/largest-series-product/example.js
trackler-2.1.0.4 tracks/ecmascript/exercises/largest-series-product/example.js
trackler-2.1.0.3 tracks/ecmascript/exercises/largest-series-product/example.js
trackler-2.1.0.2 tracks/ecmascript/exercises/largest-series-product/example.js
trackler-2.1.0.1 tracks/ecmascript/exercises/largest-series-product/example.js
trackler-2.1.0.0 tracks/ecmascript/exercises/largest-series-product/example.js
trackler-2.0.8.55 tracks/ecmascript/exercises/largest-series-product/example.js
trackler-2.0.8.54 tracks/ecmascript/exercises/largest-series-product/example.js
trackler-2.0.8.53 tracks/ecmascript/exercises/largest-series-product/example.js
trackler-2.0.8.52 tracks/ecmascript/exercises/largest-series-product/example.js
trackler-2.0.8.51 tracks/ecmascript/exercises/largest-series-product/example.js
trackler-2.0.8.50 tracks/ecmascript/exercises/largest-series-product/example.js
trackler-2.0.8.49 tracks/ecmascript/exercises/largest-series-product/example.js
trackler-2.0.8.48 tracks/ecmascript/exercises/largest-series-product/example.js
trackler-2.0.8.47 tracks/ecmascript/exercises/largest-series-product/example.js
trackler-2.0.8.46 tracks/ecmascript/exercises/largest-series-product/example.js
trackler-2.0.8.45 tracks/ecmascript/exercises/largest-series-product/example.js
trackler-2.0.8.44 tracks/ecmascript/exercises/largest-series-product/example.js
trackler-2.0.8.43 tracks/ecmascript/exercises/largest-series-product/example.js
trackler-2.0.8.42 tracks/ecmascript/exercises/largest-series-product/example.js