Sha256: 0af4986120266bebe6d0069260a495fa177efaa20946e497aeccf92d954f308a

Contents?: true

Size: 1.12 KB

Versions: 73

Compression:

Stored size: 1.12 KB

Contents

'use strict';

module.exports = Triplet;

function Triplet(a, b, c) {
  this.a = a;
  this.b = b;
  this.c = c;
}

function Triplets(conditions) {
  this.min = conditions.minFactor || 1;
  this.max = conditions.maxFactor;
  this.sum = conditions.sum;
}

Triplet.prototype.isPythagorean = function () {
  return this.a * this.a + this.b * this.b === this.c * this.c;
};

Triplet.prototype.product = function () {
  return this.a * this.b * this.c;
};

Triplet.prototype.sum = function () {
  return this.a + this.b + this.c;
};

Triplet.where = function (conditions) {
  return new Triplets(conditions).toArray();
};

Triplets.prototype.isDesired = function (triplet) {
  return triplet.isPythagorean() && (!this.sum || triplet.sum() === this.sum);
};

Triplets.prototype.toArray = function () {
  var triplet = [];
  var triplets = [];
  for (var a = this.min; a < this.max - 1; a++) {
    for (var b = a + 1; b < this.max; b++) {
      for (var c = b + 1; c <= this.max; c++) {
        triplet = new Triplet(a, b, c);
        if (this.isDesired(triplet)) {
          triplets.push(triplet);
        }
      }
    }
  }
  return triplets;
};

Version data entries

73 entries across 73 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/javascript/exercises/pythagorean-triplet/example.js
trackler-2.2.1.179 tracks/javascript/exercises/pythagorean-triplet/example.js
trackler-2.2.1.178 tracks/javascript/exercises/pythagorean-triplet/example.js
trackler-2.2.1.177 tracks/javascript/exercises/pythagorean-triplet/example.js
trackler-2.2.1.176 tracks/javascript/exercises/pythagorean-triplet/example.js
trackler-2.2.1.175 tracks/javascript/exercises/pythagorean-triplet/example.js
trackler-2.2.1.174 tracks/javascript/exercises/pythagorean-triplet/example.js
trackler-2.2.1.173 tracks/javascript/exercises/pythagorean-triplet/example.js
trackler-2.2.1.172 tracks/javascript/exercises/pythagorean-triplet/example.js
trackler-2.2.1.171 tracks/javascript/exercises/pythagorean-triplet/example.js
trackler-2.2.1.170 tracks/javascript/exercises/pythagorean-triplet/example.js
trackler-2.2.1.169 tracks/javascript/exercises/pythagorean-triplet/example.js
trackler-2.2.1.167 tracks/javascript/exercises/pythagorean-triplet/example.js
trackler-2.2.1.166 tracks/javascript/exercises/pythagorean-triplet/example.js
trackler-2.2.1.165 tracks/javascript/exercises/pythagorean-triplet/example.js
trackler-2.2.1.164 tracks/javascript/exercises/pythagorean-triplet/example.js
trackler-2.2.1.163 tracks/javascript/exercises/pythagorean-triplet/example.js
trackler-2.2.1.162 tracks/javascript/exercises/pythagorean-triplet/example.js
trackler-2.2.1.161 tracks/javascript/exercises/pythagorean-triplet/example.js
trackler-2.2.1.160 tracks/javascript/exercises/pythagorean-triplet/example.js