Exercism v3 launches on Sept 1st 2021. Learn more! ๐Ÿš€๐Ÿš€๐Ÿš€
Avatar of rootulp

rootulp's solution

to Bob in the TypeScript Track

Published at Jul 13 2018 · 0 comments
Instructions
Test suite
Solution

Note:

This solution was written on an old version of Exercism. The tests below might not correspond to the solution code, and the exercise may have changed since this code was written.

Bob is a lackadaisical teenager. In conversation, his responses are very limited.

Bob answers 'Sure.' if you ask him a question.

He answers 'Whoa, chill out!' if you yell at him.

He says 'Fine. Be that way!' if you address him without actually saying anything.

He answers 'Whatever.' to anything else.

Setup

Go through the setup instructions for TypeScript to install the necessary dependencies:

http://exercism.io/languages/typescript

Requirements

Install assignment dependencies:

$ yarn install

Making the test suite pass

Execute the tests with:

$ yarn test

Source

Inspired by the 'Deaf Grandma' exercise in Chris Pine's Learn to Program tutorial. http://pine.fm/LearnToProgram/?Chapter=06

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

bob.test.ts

import Bob from './bob'

describe('Bob', () => {

  const bob = new Bob()

  it('stating something', () => {
    const result = bob.hey('Tom-ay-to, tom-aaaah-to.')
    expect(result).toEqual('Whatever.')
  })

  xit('shouting', () => {
    const result = bob.hey('WATCH OUT!')
    expect(result).toEqual('Whoa, chill out!')
  })

  xit('asking a question', () => {
    const result = bob.hey('Does this cryogenic chamber make me look fat?')
    expect(result).toEqual('Sure.')
  })

  xit('talking forcefully', () => {
    const result = bob.hey("Let's go make out behind the gym!")
    expect(result).toEqual('Whatever.')
  })

  xit('using acronyms in regular speech', () => {
    const result = bob.hey("It's OK if you don't want to go to the DMV.")
    expect(result).toEqual('Whatever.')
  })

  xit('forceful questions', () => {
    const result = bob.hey('WHAT THE HELL WERE YOU THINKING?')
    expect(result).toEqual('Whoa, chill out!')
  })

  xit('shouting numbers', () => {
    const result = bob.hey('1, 2, 3 GO!')
    expect(result).toEqual('Whoa, chill out!')
  })

  xit('only numbers', () => {
    const result = bob.hey('1, 2, 3')
    expect(result).toEqual('Whatever.')
  })

  xit('question with only numbers', () => {
    const result = bob.hey('4?')
    expect(result).toEqual('Sure.')
  })

  xit('shouting with special characters', () => {
    const result = bob.hey('ZOMG THE %^*@#$(*^ ZOMBIES ARE COMING!!11!!1!')
    expect(result).toEqual('Whoa, chill out!')
  })

  xit('shouting with umlauts', () => {
    const result = bob.hey('\xdcML\xc4\xdcTS!')
    expect(result).toEqual('Whoa, chill out!')
  })

  xit('calmly speaking about umlauts', () => {
    const result = bob.hey('\xfcML\xe4\xdcTS')
    expect(result).toEqual('Whatever.')
  })

  xit('shouting with no exclamation mark', () => {
    const result = bob.hey('I HATE YOU')
    expect(result).toEqual('Whoa, chill out!')
  })

  xit('statement containing question mark', () => {
    const result = bob.hey('Ending with a ? means a question.')
    expect(result).toEqual('Whatever.')
  })

  xit('prattling on', () => {
    const result = bob.hey('Wait! Hang on.  Are you going to be OK?')
    expect(result).toEqual('Sure.')
  })

  xit('silence', () => {
    const result = bob.hey('')
    expect(result).toEqual('Fine. Be that way!')
  })

  xit('prolonged silence', () => {
    const result = bob.hey('   ')
    expect(result).toEqual('Fine. Be that way!')
  })

})
class Bob {
    static readonly RESPONSE_TO_YELLING = "Whoa, chill out!"
    static readonly RESPONSE_TO_QUESTION = "Sure."
    static readonly RESPONSE_TO_SILENCE = "Fine. Be that way!"
    public hey(input: string): string {
        if (this.is_yelling(input)) {
            return Bob.RESPONSE_TO_YELLING;
        } else if (this.is_question(input)) {
            return Bob.RESPONSE_TO_QUESTION;
        } else if (this.is_silence(input)) {
            return Bob.RESPONSE_TO_SILENCE;
        }
        return "Whatever.";
    }

    private is_yelling(input: string): boolean {
        return input === input.toUpperCase() && input !== input.toLowerCase();
    }

    private is_question(input: string): boolean {
        return input.endsWith("?");
    }

    private is_silence(input: string): boolean {
        return input.trim().length === 0;
    }
}

export default Bob

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other peopleโ€™s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?