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.159 tracks/typescript/exercises/triangle/triangle.example.ts
trackler-2.2.1.158 tracks/typescript/exercises/triangle/triangle.example.ts
trackler-2.2.1.157 tracks/typescript/exercises/triangle/triangle.example.ts
trackler-2.2.1.156 tracks/typescript/exercises/triangle/triangle.example.ts
trackler-2.2.1.155 tracks/typescript/exercises/triangle/triangle.example.ts
trackler-2.2.1.154 tracks/typescript/exercises/triangle/triangle.example.ts
trackler-2.2.1.153 tracks/typescript/exercises/triangle/triangle.example.ts
trackler-2.2.1.152 tracks/typescript/exercises/triangle/triangle.example.ts
trackler-2.2.1.151 tracks/typescript/exercises/triangle/triangle.example.ts
trackler-2.2.1.150 tracks/typescript/exercises/triangle/triangle.example.ts
trackler-2.2.1.149 tracks/typescript/exercises/triangle/triangle.example.ts
trackler-2.2.1.148 tracks/typescript/exercises/triangle/triangle.example.ts
trackler-2.2.1.147 tracks/typescript/exercises/triangle/triangle.example.ts
trackler-2.2.1.146 tracks/typescript/exercises/triangle/triangle.example.ts
trackler-2.2.1.145 tracks/typescript/exercises/triangle/triangle.example.ts
trackler-2.2.1.144 tracks/typescript/exercises/triangle/triangle.example.ts
trackler-2.2.1.143 tracks/typescript/exercises/triangle/triangle.example.ts
trackler-2.2.1.142 tracks/typescript/exercises/triangle/triangle.example.ts
trackler-2.2.1.141 tracks/typescript/exercises/triangle/triangle.example.ts
trackler-2.2.1.140 tracks/typescript/exercises/triangle/triangle.example.ts