 # rootulp's solution

## to Triangle in the TypeScript Track

Published at Aug 14 2019 · 0 comments
Instructions
Test suite
Solution

#### Note:

This exercise has changed since this solution was written.

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 TypeScript to install the necessary dependencies:

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

## Requirements

Install assignment dependencies:

``````\$ yarn install
``````

## Making the test suite pass

Execute the tests with:

``````\$ yarn 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 `xit` to `it`.

## 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.test.ts

``````import Triangle from './triangle'

describe('Triangle', () => {
it("equilateral triangles have equal sides", () => {
const triangle = new Triangle(2, 2, 2)
expect(triangle.kind()).toEqual("equilateral")
})

xit("larger equilateral triangles also have equal sides", () => {
const triangle = new Triangle(10, 10, 10)
expect(triangle.kind()).toEqual('equilateral')
})

xit('isosceles triangles have last two sides equal', () => {
const triangle = new Triangle(3, 4, 4)
expect(triangle.kind()).toEqual('isosceles')
})

xit('isosceles trianges have first and last sides equal', () => {
const triangle = new Triangle(4, 3, 4)
expect(triangle.kind()).toEqual('isosceles')
})

xit('isosceles triangles have two first sides equal', () => {
const triangle = new Triangle(4, 4, 3)
expect(triangle.kind()).toEqual('isosceles')
})

xit('isosceles triangles have in fact exactly two sides equal', () => {
const triangle = new Triangle(10, 10, 2)
expect(triangle.kind()).toEqual('isosceles')
})

xit('scalene triangles have no equal sides', () => {
const triangle = new Triangle(3, 4, 5)
expect(triangle.kind()).toEqual('scalene')
})

xit('scalene triangles have no equal sides at a larger scale too', () => {
const triangle = new Triangle(10, 11, 12)
expect(triangle.kind()).toEqual('scalene')
})

xit('scalene triangles have no equal sides in descending order either', () => {
const triangle = new Triangle(5, 4, 2)
expect(triangle.kind()).toEqual('scalene')
})

xit('very small triangles are legal', () => {
const triangle = new Triangle(0.4, 0.6, 0.3)
expect(triangle.kind()).toEqual('scalene')
})

xit('test triangles with no size are illegal', () => {
const triangle = new Triangle(0, 0, 0)
expect(triangle.kind.bind(triangle)).toThrow()
})

xit('triangles with negative sides are illegal', () => {
const triangle = new Triangle(3, 4, -5)
expect(triangle.kind.bind(triangle)).toThrow()
})

xit('triangles violating triangle inequality are illegal', () => {
const triangle = new Triangle(1, 1, 3)
expect(triangle.kind.bind(triangle)).toThrow()
})

xit('triangles violating triangle inequality are illegal 2', () => {
const triangle = new Triangle(7, 3, 2)
expect(triangle.kind.bind(triangle)).toThrow()
})

xit('triangles violating triangle inequality are illegal 3', () => {
const triangle = new Triangle(10, 1, 3)
expect(triangle.kind.bind(triangle)).toThrow()
})

})``````
``````export default class Triangle {

sides: number[]

constructor(...sides: number[]) {
this.sides = sides
}

public kind(): string {
if (this.is_illegal()) {
throw('illegal triangle sides');
} else if (this.is_equilateral()){
return 'equilateral'
} else if (this.is_isosceles()) {
return 'isosceles'
}
return 'scalene'
}

private is_illegal(): boolean {
return this.is_any_side_illegal() || this.is_triangle_inequality_illegal()
}

private is_any_side_illegal(): boolean {
return this.sides.some(side => side <= 0);
}

private is_triangle_inequality_illegal(): boolean {
return this.sides + this.sides < this.sides ||
this.sides + this.sides < this.sides ||
this.sides + this.sides < this.sides
}

private is_equilateral(): boolean {
return this.sides === this.sides  && this.sides === this.sides
}

private is_isosceles(): boolean {
return this.sides === this.sides ||
this.sides === this.sides ||
this.sides === this.sides
}

}``````