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.159 tracks/javascript/exercises/pythagorean-triplet/example.js
trackler-2.2.1.158 tracks/javascript/exercises/pythagorean-triplet/example.js
trackler-2.2.1.157 tracks/javascript/exercises/pythagorean-triplet/example.js
trackler-2.2.1.156 tracks/javascript/exercises/pythagorean-triplet/example.js
trackler-2.2.1.155 tracks/javascript/exercises/pythagorean-triplet/example.js
trackler-2.2.1.154 tracks/javascript/exercises/pythagorean-triplet/example.js
trackler-2.2.1.153 tracks/javascript/exercises/pythagorean-triplet/example.js
trackler-2.2.1.152 tracks/javascript/exercises/pythagorean-triplet/example.js
trackler-2.2.1.151 tracks/javascript/exercises/pythagorean-triplet/example.js
trackler-2.2.1.150 tracks/javascript/exercises/pythagorean-triplet/example.js
trackler-2.2.1.149 tracks/javascript/exercises/pythagorean-triplet/example.js
trackler-2.2.1.148 tracks/javascript/exercises/pythagorean-triplet/example.js
trackler-2.2.1.147 tracks/javascript/exercises/pythagorean-triplet/example.js
trackler-2.2.1.146 tracks/javascript/exercises/pythagorean-triplet/example.js
trackler-2.2.1.145 tracks/javascript/exercises/pythagorean-triplet/example.js
trackler-2.2.1.144 tracks/javascript/exercises/pythagorean-triplet/example.js
trackler-2.2.1.143 tracks/javascript/exercises/pythagorean-triplet/example.js
trackler-2.2.1.142 tracks/javascript/exercises/pythagorean-triplet/example.js
trackler-2.2.1.141 tracks/javascript/exercises/pythagorean-triplet/example.js
trackler-2.2.1.140 tracks/javascript/exercises/pythagorean-triplet/example.js