Avatar of LearningNerd

LearningNerd's solution

to Bob in the JavaScript Track

Published at Jul 13 2018 · 2 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 answers 'Calm down, I know what I'm doing!' if you yell a question 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 JavaScript to install the necessary dependencies:

http://exercism.io/languages/javascript/installation

Running the test suite

The provided test suite uses Jasmine. You can install it by opening a terminal window and running the following command:

npm install -g jasmine

Run the test suite from the exercise directory with:

jasmine bob.spec.js

In many test suites all but the first test have been marked "pending". Once you get a test passing, activate the next one by changing xit to it.

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.spec.js

var Bob = require('./bob.js');

describe('Bob', function () {
  var bob = new Bob();

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

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

  xit('shouting gibberish', function () {
    var result = bob.hey('FCECDFCAAB');
    expect(result).toEqual('Whoa, chill out!');
  });

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

  xit('asking a numeric question', function () {
    var result = bob.hey('You are, what, like 15?');
    expect(result).toEqual('Sure.');
  });

  xit('asking gibberish', function () {
    var result = bob.hey('fffbbcbeab?');
    expect(result).toEqual('Sure.');
  });

  xit('talking forcefully', function () {
    var result = bob.hey('Let\'s go make out behind the gym!');
    expect(result).toEqual('Whatever.');
  });

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

  xit('forceful questions', function () {
    var result = bob.hey('WHAT THE HELL WERE YOU THINKING?');
    expect(result).toEqual("Calm down, I know what I'm doing!");
  });

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

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

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

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

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

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

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

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

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

  xit('alternate silence', function () {
    var result = bob.hey('\t\t\t\t\t\t\t\t\t\t');
    expect(result).toEqual('Fine. Be that way!');
  });

  xit('multiple line question', function () {
    var result = bob.hey('\nDoes this cryogenic chamber make me look fat?\nno');
    expect(result).toEqual('Whatever.');
  });

  xit('starting with whitespace', function () {
    var result = bob.hey('         hmmmmmmm...');
    expect(result).toEqual('Whatever.');
  });

  xit('ending with whitespace', function () {
    var result = bob.hey('Okay if like my  spacebar  quite a bit?   ');
    expect(result).toEqual('Sure.');
  });

  xit('other whitespace', function () {
    var result = bob.hey('\n\r \t');
    expect(result).toEqual('Fine. Be that way!');
  });

  xit('non-question ending with whitespace', function () {
    var result = bob.hey('This is a statement ending with whitespace      ');
    expect(result).toEqual('Whatever.');
  });
});
// This is only a SKELETON file for the "Bob" exercise. It's been provided as a
// convenience to get you started writing code faster.

var Bob = function() {};

Bob.prototype.hey = function(input) {

	// silence: if input is empty or contains nothing but empty characters
	if( !input.trim() ) {
       return 'Fine. Be that way!';
	// shouting: if all characters are uppercase and the string contains letters
	} else if( input == input.toUpperCase() && /[a-z]/i.test(input) ) {
		return 'Whoa, chill out!';  
	// question: if last character ends in '?'
	} else if( input.slice(-1) == '?' ) {
       return 'Sure.';
	// default response for a teenager:
	} else {
		return 'Whatever.';
	}
};

module.exports = Bob;

Community comments

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

I like how elegantly you did the shouting detection with input == input.toUpperCase(), it's so simple yet didn't even occur to me. :-)

One suggestion: I like to replace comments like the ones in your code with calls to helper functions with descriptive names. I find this easier to read, because you don't have to look at all details at once; instead, you get a high-level overview of how the "main" function works first and then can look into the actual implementation details of the parts afterwards. Here is an example of what I'm talking about: Bob.prototype.hey = function(input) { if (isSilence(input)) { return 'Fine. Be that way!'; } else if (isShouting(input) { return 'Whoa, chill out!'; } ... }

function isSilence(input) { return !input.trim(); }

function isShouting(input) { ... }

...

Maybe you like this style as well.

Avatar of LearningNerd
LearningNerd
Solution Author
commented almost 5 years ago

Thanks @denisw! That's a great suggestion and you explained its advantages very well! In this particular example, I think it's OK not to use helper functions since it's such a small amount of code. But I can see how those functions would make this code much more extensible and easier for collaborating with others.

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?