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