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