Sha256: c4a26fc8fd2e72c49a0b85f7887e8236b0c24c3167b040c80cb997e45496581b

Contents?: true

Size: 1.7 KB

Versions: 283

Compression:

Stored size: 1.7 KB

Contents

const animals = ["fly", "spider", "bird", "cat", "dog", "goat", "cow", "horse"]

export default class FoodChain {

    public static verses(start = 1, end = 8): string {
        if (end < start) {throw new Error("end should be smaller than the start")}
        let verses = ""

        for ( ; start <= end; start += 1) {
            verses += FoodChain.verse(start)
            if (start !== end) {
                verses += "\n"
            }
        }
        return verses
    }

    public static verse(num: number): string {
        let result = ""

        let index = num - 1
        result += `I know an old lady who swallowed a ${animals[index]}.\n`

        switch (num) {
        case 2: result += "It wriggled and jiggled and tickled inside her.\n"
            break
        case 3: result += "How absurd to swallow a bird!\n"
            break
        case 4: result += "Imagine that, to swallow a cat!\n"
            break
        case 5: result += "What a hog, to swallow a dog!\n"
            break
        case 6: result += "Just opened her throat and swallowed a goat!\n"
            break
        case 7: result += "I don't know how she swallowed a cow!\n"
            break
        case 8: result += "She's dead, of course!\n"
            return result
        default: break
        }

        while ( index >= 1 ) {
            result += `She swallowed the ${animals[index]} to catch the ${animals[index - 1]}`
            if ( index === 2 ) {
                result += " that wriggled and jiggled and tickled inside her"
            }
            result += ".\n"
            index -= 1
        }
        result += "I don't know why she swallowed the fly. Perhaps she'll die."

        return result + "\n"
    }
}

Version data entries

283 entries across 283 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/typescript/exercises/food-chain/food-chain.example.ts
trackler-2.2.1.179 tracks/typescript/exercises/food-chain/food-chain.example.ts
trackler-2.2.1.178 tracks/typescript/exercises/food-chain/food-chain.example.ts
trackler-2.2.1.177 tracks/typescript/exercises/food-chain/food-chain.example.ts
trackler-2.2.1.176 tracks/typescript/exercises/food-chain/food-chain.example.ts
trackler-2.2.1.175 tracks/typescript/exercises/food-chain/food-chain.example.ts
trackler-2.2.1.174 tracks/typescript/exercises/food-chain/food-chain.example.ts
trackler-2.2.1.173 tracks/typescript/exercises/food-chain/food-chain.example.ts
trackler-2.2.1.172 tracks/typescript/exercises/food-chain/food-chain.example.ts
trackler-2.2.1.171 tracks/typescript/exercises/food-chain/food-chain.example.ts
trackler-2.2.1.170 tracks/typescript/exercises/food-chain/food-chain.example.ts
trackler-2.2.1.169 tracks/typescript/exercises/food-chain/food-chain.example.ts
trackler-2.2.1.167 tracks/typescript/exercises/food-chain/food-chain.example.ts
trackler-2.2.1.166 tracks/typescript/exercises/food-chain/food-chain.example.ts
trackler-2.2.1.165 tracks/typescript/exercises/food-chain/food-chain.example.ts
trackler-2.2.1.164 tracks/typescript/exercises/food-chain/food-chain.example.ts
trackler-2.2.1.163 tracks/typescript/exercises/food-chain/food-chain.example.ts
trackler-2.2.1.162 tracks/typescript/exercises/food-chain/food-chain.example.ts
trackler-2.2.1.161 tracks/typescript/exercises/food-chain/food-chain.example.ts
trackler-2.2.1.160 tracks/typescript/exercises/food-chain/food-chain.example.ts