Avatar of w1zeman1p

w1zeman1p's solution

to Bob in the JavaScript 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 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.');
  });
});
var Bob = function() {};

var isQuestion = function(input) {
    return input[input.length - 1] === '?';
};

var isSilence = function(input) {
    return input.trim() === '';
};

var isShouting = function(input) {
    var capitals = 0, lowercases = 0, i = 0, ascii;
    for(; i < input.length; i++) {
        // Skip if it's a number or special character
        if(input[i].match(/[~?!@#$%^&*)(.,0-9 ]/)) {
            continue;
        }
        if(input[i].toLocaleLowerCase() === input[i]) {
            lowercases++;
        } else {
            capitals++;
        }
    }
    return lowercases === 0 && capitals > 0;
};

Bob.prototype.hey = function(input) {
    if(isShouting(input)) {
        return 'Whoa, chill out!';
    }
    if(isQuestion(input)) {
        return 'Sure.';
    }
    if(isSilence(input)) {
        return 'Fine. Be that way!';
    }
    return 'Whatever.';
};

module.exports = 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?