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