Sha256: 24a30b980604d1c6d7caa73772fb79e322c4b08cd4b7c118abd848c9ee9016ca

Contents?: true

Size: 1.05 KB

Versions: 119

Compression:

Stored size: 1.05 KB

Contents

'use strict';

function Series(numberString) {
  if (numberString.match('[^0-9]')) throw new Error('Invalid input.');
  this.numberString = numberString;
  this.digits = this.getDigits();
}

Series.prototype.getDigits = function () {
  return this.numberString.split('').map(function (digit) {
    return parseInt(digit, 10);
  });
};

Series.prototype.largestProduct = function (size) {
  if (size < 0) throw new Error('Invalid input.');
  var product;
  var max = 0;
  this.slices(size).forEach(function (slice) {
    product = slice.reduce(function (a, b) {
      return a * b;
    }, 1);
    if (product > max) { max = product; }
  });
  return max;
};

Series.prototype.slices = function (sliceSize) {
  var result = [];
  var slice = [];

  if (sliceSize > this.digits.length) {
    throw new Error('Slice size is too big.');
  }

  for (var i = 0; i < this.digits.length - sliceSize + 1; i++) {
    for (var j = 0; j < sliceSize; j++) {
      slice.push(this.digits[i + j]);
    }
    result.push(slice);
    slice =  [];
  }

  return result;
};

module.exports = Series;

Version data entries

119 entries across 119 versions & 1 rubygems

Version Path
trackler-2.2.1.139 tracks/javascript/exercises/largest-series-product/example.js
trackler-2.2.1.138 tracks/javascript/exercises/largest-series-product/example.js
trackler-2.2.1.137 tracks/javascript/exercises/largest-series-product/example.js
trackler-2.2.1.136 tracks/javascript/exercises/largest-series-product/example.js
trackler-2.2.1.135 tracks/javascript/exercises/largest-series-product/example.js
trackler-2.2.1.134 tracks/javascript/exercises/largest-series-product/example.js
trackler-2.2.1.133 tracks/javascript/exercises/largest-series-product/example.js
trackler-2.2.1.132 tracks/javascript/exercises/largest-series-product/example.js
trackler-2.2.1.131 tracks/javascript/exercises/largest-series-product/example.js
trackler-2.2.1.130 tracks/javascript/exercises/largest-series-product/example.js
trackler-2.2.1.129 tracks/javascript/exercises/largest-series-product/example.js
trackler-2.2.1.128 tracks/javascript/exercises/largest-series-product/example.js
trackler-2.2.1.127 tracks/javascript/exercises/largest-series-product/example.js
trackler-2.2.1.126 tracks/javascript/exercises/largest-series-product/example.js
trackler-2.2.1.125 tracks/javascript/exercises/largest-series-product/example.js
trackler-2.2.1.124 tracks/javascript/exercises/largest-series-product/example.js
trackler-2.2.1.123 tracks/javascript/exercises/largest-series-product/example.js
trackler-2.2.1.122 tracks/javascript/exercises/largest-series-product/example.js
trackler-2.2.1.121 tracks/javascript/exercises/largest-series-product/example.js
trackler-2.2.1.120 tracks/javascript/exercises/largest-series-product/example.js