Sha256: b800a30f936b099079d5ccacc3b761279477f347c6687b75aa10acda968ad712

Contents?: true

Size: 1.32 KB

Versions: 236

Compression:

Stored size: 1.32 KB

Contents

class ArgumentError {}

class WordProblem {
    question: string
    constructor(question: string) {
        this.question = question
    }

    answer(): number {
        type MathFunc = (a: number, b: number) => number
         const operans: {[key: string]: MathFunc} = { plus : (a: number, b: number) => {return a + b},
                        minus : (a: number, b: number) => {return a - b},
                        multiplied : (a: number, b: number) => {return a * b},
                        divided : (a: number, b: number) => {return a / b} }

        const querry = this.question.replace(/by /g, "").replace("What is ", "").replace("?", "")
        const array = querry.split(" ")

        let subtotal = 0

        if (array.length > 5 || array.length < 3 ) { throw new ArgumentError()}

        if (array.length >= 3) {
            const a = array[0]
            const b: MathFunc = operans[array[1]]
            const c = array[2]
            if (b === undefined) {throw new ArgumentError()}
            subtotal =  b(+a, +c)
        }
        if (array.length === 5) {
            const d: MathFunc = operans[array[3]]
            const e = array[4]
            if (d === undefined) {throw new ArgumentError()}
            subtotal = d(subtotal, +e)
        }
        return subtotal
    }
}

export { ArgumentError, WordProblem }

Version data entries

236 entries across 236 versions & 1 rubygems

Version Path
trackler-2.2.1.139 tracks/typescript/exercises/wordy/wordy.example.ts
trackler-2.2.1.138 tracks/typescript/exercises/wordy/wordy.example.ts
trackler-2.2.1.137 tracks/typescript/exercises/wordy/wordy.example.ts
trackler-2.2.1.136 tracks/typescript/exercises/wordy/wordy.example.ts
trackler-2.2.1.135 tracks/typescript/exercises/wordy/wordy.example.ts
trackler-2.2.1.134 tracks/typescript/exercises/wordy/wordy.example.ts
trackler-2.2.1.133 tracks/typescript/exercises/wordy/wordy.example.ts
trackler-2.2.1.132 tracks/typescript/exercises/wordy/wordy.example.ts
trackler-2.2.1.131 tracks/typescript/exercises/wordy/wordy.example.ts
trackler-2.2.1.130 tracks/typescript/exercises/wordy/wordy.example.ts
trackler-2.2.1.129 tracks/typescript/exercises/wordy/wordy.example.ts
trackler-2.2.1.128 tracks/typescript/exercises/wordy/wordy.example.ts
trackler-2.2.1.127 tracks/typescript/exercises/wordy/wordy.example.ts
trackler-2.2.1.126 tracks/typescript/exercises/wordy/wordy.example.ts
trackler-2.2.1.125 tracks/typescript/exercises/wordy/wordy.example.ts
trackler-2.2.1.124 tracks/typescript/exercises/wordy/wordy.example.ts
trackler-2.2.1.123 tracks/typescript/exercises/wordy/wordy.example.ts
trackler-2.2.1.122 tracks/typescript/exercises/wordy/wordy.example.ts
trackler-2.2.1.121 tracks/typescript/exercises/wordy/wordy.example.ts
trackler-2.2.1.120 tracks/typescript/exercises/wordy/wordy.example.ts