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.119 tracks/javascript/exercises/triangle/example.js
trackler-2.2.1.118 tracks/javascript/exercises/triangle/example.js
trackler-2.2.1.117 tracks/javascript/exercises/triangle/example.js
trackler-2.2.1.116 tracks/javascript/exercises/triangle/example.js
trackler-2.2.1.115 tracks/javascript/exercises/triangle/example.js
trackler-2.2.1.114 tracks/javascript/exercises/triangle/example.js
trackler-2.2.1.113 tracks/javascript/exercises/triangle/example.js
trackler-2.2.1.111 tracks/javascript/exercises/triangle/example.js
trackler-2.2.1.110 tracks/javascript/exercises/triangle/example.js
trackler-2.2.1.109 tracks/javascript/exercises/triangle/example.js
trackler-2.2.1.108 tracks/javascript/exercises/triangle/example.js
trackler-2.2.1.107 tracks/javascript/exercises/triangle/example.js
trackler-2.2.1.106 tracks/javascript/exercises/triangle/example.js
trackler-2.2.1.105 tracks/javascript/exercises/triangle/example.js
trackler-2.2.1.104 tracks/javascript/exercises/triangle/example.js
trackler-2.2.1.103 tracks/javascript/exercises/triangle/example.js
trackler-2.2.1.102 tracks/javascript/exercises/triangle/example.js
trackler-2.2.1.101 tracks/javascript/exercises/triangle/example.js
trackler-2.2.1.100 tracks/javascript/exercises/triangle/example.js
trackler-2.2.1.99 tracks/javascript/exercises/triangle/example.js