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.119 tracks/javascript/exercises/pythagorean-triplet/example.js
trackler-2.2.1.118 tracks/javascript/exercises/pythagorean-triplet/example.js
trackler-2.2.1.117 tracks/javascript/exercises/pythagorean-triplet/example.js
trackler-2.2.1.116 tracks/javascript/exercises/pythagorean-triplet/example.js
trackler-2.2.1.115 tracks/javascript/exercises/pythagorean-triplet/example.js
trackler-2.2.1.114 tracks/javascript/exercises/pythagorean-triplet/example.js
trackler-2.2.1.113 tracks/javascript/exercises/pythagorean-triplet/example.js
trackler-2.2.1.111 tracks/javascript/exercises/pythagorean-triplet/example.js
trackler-2.2.1.110 tracks/javascript/exercises/pythagorean-triplet/example.js
trackler-2.2.1.109 tracks/javascript/exercises/pythagorean-triplet/example.js
trackler-2.2.1.108 tracks/javascript/exercises/pythagorean-triplet/example.js
trackler-2.2.1.107 tracks/javascript/exercises/pythagorean-triplet/example.js
trackler-2.2.1.106 tracks/javascript/exercises/pythagorean-triplet/example.js