Sha256: b3a9817bb2cf840101b0fffa80e8913fa02e0b4d49cf331eadb6f9fc42896af6

Contents?: true

Size: 1.26 KB

Versions: 255

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() {
    var a = this.sides[0], b = this.sides[1], c = this.sides[2];
    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) {
      uniqueSides.push(uniqueSide);
    }

    return uniqueSides;
  };
}

module.exports = Triangle;

Version data entries

255 entries across 255 versions & 1 rubygems

Version Path
trackler-2.2.1.37 tracks/javascript/exercises/triangle/example.js
trackler-2.2.1.36 tracks/javascript/exercises/triangle/example.js
trackler-2.2.1.35 tracks/javascript/exercises/triangle/example.js
trackler-2.2.1.34 tracks/javascript/exercises/triangle/example.js
trackler-2.2.1.33 tracks/javascript/exercises/triangle/example.js
trackler-2.2.1.32 tracks/javascript/exercises/triangle/example.js
trackler-2.2.1.31 tracks/javascript/exercises/triangle/example.js
trackler-2.2.1.30 tracks/javascript/exercises/triangle/example.js
trackler-2.2.1.29 tracks/javascript/exercises/triangle/example.js
trackler-2.2.1.28 tracks/javascript/exercises/triangle/example.js
trackler-2.2.1.27 tracks/javascript/exercises/triangle/example.js
trackler-2.2.1.26 tracks/javascript/exercises/triangle/example.js
trackler-2.2.1.25 tracks/javascript/exercises/triangle/example.js
trackler-2.2.1.24 tracks/javascript/exercises/triangle/example.js
trackler-2.2.1.23 tracks/javascript/exercises/triangle/example.js
trackler-2.2.1.22 tracks/javascript/exercises/triangle/example.js
trackler-2.2.1.21 tracks/javascript/exercises/triangle/example.js
trackler-2.2.1.20 tracks/javascript/exercises/triangle/example.js
trackler-2.2.1.19 tracks/javascript/exercises/triangle/example.js
trackler-2.2.1.18 tracks/javascript/exercises/triangle/example.js