Created
August 20, 2014 21:51
-
-
Save stevekinney/8eb02e495a0c29096294 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// The first thing I want to do is create a Bob constructor for instantiating | |
// new Bob objects. I don't need to do any initializing, so this is pretty much | |
// a empty constructor. | |
function Bob() {}; | |
// I'm going to store all of the functionality in Bob's prototype. This way, | |
// all of my Bob instances will share the same set of methods. | |
// Theoretically, I could use a conditional or a switch statement here, but | |
// I've chosen to just return out of the function if the condition is met. | |
Bob.prototype.hey = function (statement) { | |
if (isShouting(statement)) return 'Woah, chill out!'; | |
if (isQuestion(statement)) return 'Sure.'; | |
if (isSilent(statement)) return 'Fine. Be that way!'; | |
return 'Whatever.'; | |
}; | |
Bob.prototype.isTeenager = function () { return true; }; | |
Bob.prototype.isAdult = function () { return !this.isTeenager(); }; | |
// I've abstracted all of my conditional checks into methods because it's | |
// self-documenting. Each function returns a boolean. Unfortunately, I | |
// can't use a "?" in the method name like you can in Ruby. | |
function hasLetters(statement) { | |
return statement.match(/[A-Za-z]/); | |
}; | |
function isUpperCase(statement) { | |
return statement.toUpperCase() === statement; | |
}; | |
function isShouting(statement) { | |
return hasLetters(statement) && isUpperCase(statement); | |
}; | |
function isQuestion(statement) { | |
return statement.slice(-1) === '?'; | |
}; | |
function isSilent(statement) { | |
return !statement.trim(); | |
}; | |
module.exports = Bob; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment