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.139 tracks/javascript/exercises/pythagorean-triplet/example.js
trackler-2.2.1.138 tracks/javascript/exercises/pythagorean-triplet/example.js
trackler-2.2.1.137 tracks/javascript/exercises/pythagorean-triplet/example.js
trackler-2.2.1.136 tracks/javascript/exercises/pythagorean-triplet/example.js
trackler-2.2.1.135 tracks/javascript/exercises/pythagorean-triplet/example.js
trackler-2.2.1.134 tracks/javascript/exercises/pythagorean-triplet/example.js
trackler-2.2.1.133 tracks/javascript/exercises/pythagorean-triplet/example.js
trackler-2.2.1.132 tracks/javascript/exercises/pythagorean-triplet/example.js
trackler-2.2.1.131 tracks/javascript/exercises/pythagorean-triplet/example.js
trackler-2.2.1.130 tracks/javascript/exercises/pythagorean-triplet/example.js
trackler-2.2.1.129 tracks/javascript/exercises/pythagorean-triplet/example.js
trackler-2.2.1.128 tracks/javascript/exercises/pythagorean-triplet/example.js
trackler-2.2.1.127 tracks/javascript/exercises/pythagorean-triplet/example.js
trackler-2.2.1.126 tracks/javascript/exercises/pythagorean-triplet/example.js
trackler-2.2.1.125 tracks/javascript/exercises/pythagorean-triplet/example.js
trackler-2.2.1.124 tracks/javascript/exercises/pythagorean-triplet/example.js
trackler-2.2.1.123 tracks/javascript/exercises/pythagorean-triplet/example.js
trackler-2.2.1.122 tracks/javascript/exercises/pythagorean-triplet/example.js
trackler-2.2.1.121 tracks/javascript/exercises/pythagorean-triplet/example.js
trackler-2.2.1.120 tracks/javascript/exercises/pythagorean-triplet/example.js