Sha256: 7b081f7b9a905d2235680ed70c6087c4da5073514c8bfba9b828df50d828e5de

Contents?: true

Size: 1.32 KB

Versions: 59

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

59 entries across 59 versions & 1 rubygems

Version Path
trackler-2.1.0.4 tracks/typescript/exercises/wordy/wordy.example.ts
trackler-2.1.0.3 tracks/typescript/exercises/wordy/wordy.example.ts
trackler-2.1.0.2 tracks/typescript/exercises/wordy/wordy.example.ts
trackler-2.1.0.1 tracks/typescript/exercises/wordy/wordy.example.ts
trackler-2.1.0.0 tracks/typescript/exercises/wordy/wordy.example.ts
trackler-2.0.8.55 tracks/typescript/exercises/wordy/wordy.example.ts
trackler-2.0.8.54 tracks/typescript/exercises/wordy/wordy.example.ts
trackler-2.0.8.53 tracks/typescript/exercises/wordy/wordy.example.ts
trackler-2.0.8.52 tracks/typescript/exercises/wordy/wordy.example.ts
trackler-2.0.8.51 tracks/typescript/exercises/wordy/wordy.example.ts
trackler-2.0.8.50 tracks/typescript/exercises/wordy/wordy.example.ts
trackler-2.0.8.49 tracks/typescript/exercises/wordy/wordy.example.ts
trackler-2.0.8.48 tracks/typescript/exercises/wordy/wordy.example.ts
trackler-2.0.8.47 tracks/typescript/exercises/wordy/wordy.example.ts
trackler-2.0.8.46 tracks/typescript/exercises/wordy/wordy.example.ts
trackler-2.0.8.45 tracks/typescript/exercises/wordy/wordy.example.ts
trackler-2.0.8.44 tracks/typescript/exercises/wordy/wordy.example.ts
trackler-2.0.8.43 tracks/typescript/exercises/wordy/wordy.example.ts
trackler-2.0.8.42 tracks/typescript/exercises/wordy/wordy.example.ts
trackler-2.0.8.41 tracks/typescript/exercises/wordy/wordy.example.ts