Avatar of freddie2025

freddie2025's solution

to Triangle in the JavaScript Track

Published at Mar 28 2020 · 0 comments
Instructions
Test suite
Solution

Determine if a triangle is equilateral, isosceles, or scalene.

An equilateral triangle has all three sides the same length.

An isosceles triangle has at least two sides the same length. (It is sometimes specified as having exactly two sides the same length, but for the purposes of this exercise we'll say at least two.)

A scalene triangle has all sides of different lengths.

Note

For a shape to be a triangle at all, all sides have to be of length > 0, and the sum of the lengths of any two sides must be greater than or equal to the length of the third side. See Triangle Inequality.

Dig Deeper

The case where the sum of the lengths of two sides equals that of the third is known as a degenerate triangle - it has zero area and looks like a single line. Feel free to add your own code/tests to check for degenerate triangles.

Setup

Go through the setup instructions for Javascript to install the necessary dependencies:

https://exercism.io/tracks/javascript/installation

Requirements

Install assignment dependencies:

$ npm install

Making the test suite pass

Execute the tests with:

$ npm test

In the test suites all tests but the first have been skipped.

Once you get a test passing, you can enable the next one by changing xtest to test.

Source

The Ruby Koans triangle project, parts 1 & 2 http://rubykoans.com

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

triangle.spec.js

import { Triangle } from './triangle';

describe('Triangle', () => {
  describe('equilateral triangle', () => {
    test('all sides are equal', () => {
      const triangle = new Triangle(2, 2, 2);
      expect(triangle.isEquilateral()).toBe(true);
    });

    xtest('any side is unequal', () => {
      const triangle = new Triangle(2, 3, 2);
      expect(triangle.isEquilateral()).toBe(false);
    });

    xtest('no sides are equal', () => {
      const triangle = new Triangle(5, 4, 6);
      expect(triangle.isEquilateral()).toBe(false);
    });

    xtest('all zero sides is not a triangle', () => {
      const triangle = new Triangle(0, 0, 0);
      expect(triangle.isEquilateral()).toBe(false);
    });

    xtest('sides may be floats', () => {
      const triangle = new Triangle(0.5, 0.5, 0.5);
      expect(triangle.isEquilateral()).toBe(true);
    });
  });

  describe('isosceles triangle', () => {
    xtest('last two sides are equal', () => {
      const triangle = new Triangle(3, 4, 4);
      expect(triangle.isIsosceles()).toBe(true);
    });

    xtest('first two sides are equal', () => {
      const triangle = new Triangle(4, 4, 3);
      expect(triangle.isIsosceles()).toBe(true);
    });

    xtest('first and last sides are equal', () => {
      const triangle = new Triangle(4, 3, 4);
      expect(triangle.isIsosceles()).toBe(true);
    });

    xtest('equilateral triangles are also isosceles', () => {
      const triangle = new Triangle(4, 4, 4);
      expect(triangle.isIsosceles()).toBe(true);
    });

    xtest('no sides are equal', () => {
      const triangle = new Triangle(2, 3, 4);
      expect(triangle.isIsosceles()).toBe(false);
    });

    xtest('first triangle inequality violation', () => {
      const triangle = new Triangle(1, 1, 3);
      expect(triangle.isIsosceles()).toBe(false);
    });

    xtest('second triangle inequality violation', () => {
      const triangle = new Triangle(1, 3, 1);
      expect(triangle.isIsosceles()).toBe(false);
    });

    xtest('third triangle inequality violation', () => {
      const triangle = new Triangle(3, 1, 1);
      expect(triangle.isIsosceles()).toBe(false);
    });

    xtest('sides may be floats', () => {
      const triangle = new Triangle(0.5, 0.4, 0.5);
      expect(triangle.isIsosceles()).toBe(true);
    });
  });

  describe('scalene triangle', () => {
    xtest('no sides are equal', () => {
      const triangle = new Triangle(5, 4, 6);
      expect(triangle.isScalene()).toBe(true);
    });

    xtest('all sides are equal', () => {
      const triangle = new Triangle(4, 4, 4);
      expect(triangle.isScalene()).toBe(false);
    });

    xtest('two sides are equal', () => {
      const triangle = new Triangle(4, 4, 3);
      expect(triangle.isScalene()).toBe(false);
    });

    xtest('may not violate triangle inequality', () => {
      const triangle = new Triangle(7, 3, 2);
      expect(triangle.isScalene()).toBe(false);
    });

    xtest('sides may be floats', () => {
      const triangle = new Triangle(0.5, 0.4, 0.6);
      expect(triangle.isScalene()).toBe(true);
    });
  });
});
//
// This is only a SKELETON file for the 'Triangle' exercise. It's been provided as a
// convenience to get you started writing code faster.
//

export class Triangle {
  constructor(sideA, sideB, sideC) {
    this.sideA = sideA;
    this.sideB = sideB;
    this.sideC = sideC;
  }

  isEquilateral() {
    return this.isValidTriangle() && this.sideA === this.sideB && this.sideA === this.sideC;
  }

  isIsosceles() {
    return this.isValidTriangle() && this.calculateUniqueSides() <= 2;
  }

  isScalene() {
    return this.isValidTriangle() && this.calculateUniqueSides() === 3;
  }

  isValidTriangle() {
    return this.checkInequality() && this.sideA > 0 && this.sideB > 0 && this.sideC > 0;
  }

  calculateUniqueSides() {
    let sides = new Set();
    sides.add(this.sideA).add(this.sideB).add(this.sideC);
    return sides.size; 
  }

  checkInequality() {
    return (this.sideA + this.sideB) >= this.sideC && (this.sideA + this.sideC) >= this.sideB && (this.sideB + this.sideC) >= this.sideA;
  }

  checkDegenerate() {
    return (this.sideA + this.sideB) == this.sideC || (this.sideA + this.sideC) == this.sideB || (this.sideB + this.sideC) == this.sideA;
  }
}

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?