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