Sha256: 2d94550020d5d52088e038624790091b9bcccf866c494ab7e3d91c3a3d9ce000

Contents?: true

Size: 1.05 KB

Versions: 134

Compression:

Stored size: 1.05 KB

Contents

enum Type {
    Equilateral = "equilateral",
    Isosceles = "isosceles",
    Scalene = "scalene"
}

export default class Triangle {
    sides: number[]

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

    kind() {
        if (this.isIllegal()) {
            throw new TypeError('illegal')
        }

        if (this.isEquilateral()) {
            return Type.Equilateral
        }

        if (this.isIsosceles()) {
            return Type.Isosceles
        }

        return Type.Scalene
    }

    isIllegal() {
        return this.violatesInequality() || this.hasImpossibleSides()
    }

    violatesInequality() {
        const [a, b, c] = this.sides
        return a + b < c || a + c < b || b + c < a
    }

    hasImpossibleSides() {
        const [a, b, c] = this.sides
        return a <= 0 || b <= 0 || c <= 0
    }

    isEquilateral() {
        return this.uniqueSidesLength() === 1
    }

    isIsosceles() {
        return this.uniqueSidesLength() === 2
    }

    uniqueSidesLength() {
        return new Set(this.sides).size
    }

}

Version data entries

134 entries across 134 versions & 1 rubygems

Version Path
trackler-2.2.1.119 tracks/typescript/exercises/triangle/triangle.example.ts
trackler-2.2.1.118 tracks/typescript/exercises/triangle/triangle.example.ts
trackler-2.2.1.117 tracks/typescript/exercises/triangle/triangle.example.ts
trackler-2.2.1.116 tracks/typescript/exercises/triangle/triangle.example.ts
trackler-2.2.1.115 tracks/typescript/exercises/triangle/triangle.example.ts
trackler-2.2.1.114 tracks/typescript/exercises/triangle/triangle.example.ts
trackler-2.2.1.113 tracks/typescript/exercises/triangle/triangle.example.ts
trackler-2.2.1.111 tracks/typescript/exercises/triangle/triangle.example.ts
trackler-2.2.1.110 tracks/typescript/exercises/triangle/triangle.example.ts
trackler-2.2.1.109 tracks/typescript/exercises/triangle/triangle.example.ts
trackler-2.2.1.108 tracks/typescript/exercises/triangle/triangle.example.ts
trackler-2.2.1.107 tracks/typescript/exercises/triangle/triangle.example.ts
trackler-2.2.1.106 tracks/typescript/exercises/triangle/triangle.example.ts
trackler-2.2.1.105 tracks/typescript/exercises/triangle/triangle.example.ts
trackler-2.2.1.104 tracks/typescript/exercises/triangle/triangle.example.ts
trackler-2.2.1.103 tracks/typescript/exercises/triangle/triangle.example.ts
trackler-2.2.1.102 tracks/typescript/exercises/triangle/triangle.example.ts
trackler-2.2.1.101 tracks/typescript/exercises/triangle/triangle.example.ts
trackler-2.2.1.100 tracks/typescript/exercises/triangle/triangle.example.ts
trackler-2.2.1.99 tracks/typescript/exercises/triangle/triangle.example.ts