Sha256: 0d344ac0f7a174267353231c3adc6ed223beb54733d1fb65f7bfa5dd5ae32aed

Contents?: true

Size: 1.26 KB

Versions: 105

Compression:

Stored size: 1.26 KB

Contents

function Triangle(a, b, c) {
  'use strict';

  this.sides = [ a, b, c ];

  this.kind = function () {
    var name = 'scalene';

    if (this.isIllegal()) {
      throw new TypeError('illegal');
    } else if (this.isEquilateral()) {
      name = 'equilateral';
    } else if (this.isIsosceles()) {
      name = 'isosceles';
    }

    return name;
  };

  this.isIllegal = function () {
    return this.violatesInequality() || this.hasImpossibleSides();
  };

  this.violatesInequality = function () {
    return (a + b < c) || (a + c < b) || (b + c < a);
  };

  this.hasImpossibleSides = function () {
    return this.sides[0] <= 0 || this.sides[1] <= 0 || this.sides[2] <= 0;
  };

  this.isEquilateral = function () {
    return this.uniqueSides().length === 1;
  };

  this.isIsosceles = function () {
    return this.uniqueSides().length === 2;
  };

  this.uniqueSides = function () {
    var sides = this.sides;
    var uniques = {};

    for (var i = 0; i < sides.length; i++) {
      var currentSide = sides[i];
      uniques[currentSide] = true;
    }

    var uniqueSides = [];

    for (var uniqueSide in uniques) {
      if (uniques.hasOwnProperty(uniqueSide)) {
        uniqueSides.push(uniqueSide);
      }
    }

    return uniqueSides;
  };
}

module.exports = Triangle;

Version data entries

105 entries across 105 versions & 1 rubygems

Version Path
trackler-2.2.1.78 tracks/javascript/exercises/triangle/example.js
trackler-2.2.1.77 tracks/javascript/exercises/triangle/example.js
trackler-2.2.1.76 tracks/javascript/exercises/triangle/example.js
trackler-2.2.1.75 tracks/javascript/exercises/triangle/example.js
trackler-2.2.1.74 tracks/javascript/exercises/triangle/example.js