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.180 tracks/typescript/exercises/triangle/triangle.example.ts
trackler-2.2.1.179 tracks/typescript/exercises/triangle/triangle.example.ts
trackler-2.2.1.178 tracks/typescript/exercises/triangle/triangle.example.ts
trackler-2.2.1.177 tracks/typescript/exercises/triangle/triangle.example.ts
trackler-2.2.1.176 tracks/typescript/exercises/triangle/triangle.example.ts
trackler-2.2.1.175 tracks/typescript/exercises/triangle/triangle.example.ts
trackler-2.2.1.174 tracks/typescript/exercises/triangle/triangle.example.ts
trackler-2.2.1.173 tracks/typescript/exercises/triangle/triangle.example.ts
trackler-2.2.1.172 tracks/typescript/exercises/triangle/triangle.example.ts
trackler-2.2.1.171 tracks/typescript/exercises/triangle/triangle.example.ts
trackler-2.2.1.170 tracks/typescript/exercises/triangle/triangle.example.ts
trackler-2.2.1.169 tracks/typescript/exercises/triangle/triangle.example.ts
trackler-2.2.1.167 tracks/typescript/exercises/triangle/triangle.example.ts
trackler-2.2.1.166 tracks/typescript/exercises/triangle/triangle.example.ts
trackler-2.2.1.165 tracks/typescript/exercises/triangle/triangle.example.ts
trackler-2.2.1.164 tracks/typescript/exercises/triangle/triangle.example.ts
trackler-2.2.1.163 tracks/typescript/exercises/triangle/triangle.example.ts
trackler-2.2.1.162 tracks/typescript/exercises/triangle/triangle.example.ts
trackler-2.2.1.161 tracks/typescript/exercises/triangle/triangle.example.ts
trackler-2.2.1.160 tracks/typescript/exercises/triangle/triangle.example.ts