Skip to content

Instantly share code, notes, and snippets.

@xpmatteo
Last active December 1, 2017 10:05
Show Gist options
  • Save xpmatteo/8317762 to your computer and use it in GitHub Desktop.
Save xpmatteo/8317762 to your computer and use it in GitHub Desktop.
Solution of the FizzBuzz problem in an OCP dojo
describe('fizz buzz',function(){
var self = this;
beforeEach(function(){
var threeChecker = new Checker(3, "Fizz");
var fiveChecker = new Checker(5, "Buzz");
var sevenChecker = new Checker(7, "Bang");
self.fizzBuzzer = new FizzBuzzer([threeChecker, fiveChecker, sevenChecker]);
});
it('Default is say the number',function(){
expect(self.fizzBuzzer.say(1)).toBe('1');
});
it('If the number is multiple of 3 say Fizz',function(){
expect(self.fizzBuzzer.say(3)).toBe('Fizz');
});
it('If the number is multiple of 5 say Buzz',function(){
expect(self.fizzBuzzer.say(5)).toBe('Buzz');
});
it ('FizzBuzz', function(){
expect(self.fizzBuzzer.say(15)).toBe('FizzBuzz');
});
it ('Bang', function(){
expect(self.fizzBuzzer.say(7)).toBe('Bang');
});
it ('FizzBang', function(){
expect(self.fizzBuzzer.say(21)).toBe('FizzBang');
});
});
var Checker = (function(Checker){
Checker = function(number, result) {
this.number = number;
this.result = result;
};
Checker.prototype.check = function(n) {
return (n%this.number === 0) ? this.result: "";
};
return Checker;
}());
function FizzBuzzer(checkers) {
this.checkers = checkers;
};
FizzBuzzer.prototype.say = function(n) {
var results = [];
this.checkers.forEach(function(checker) {
results.push(checker.check(n));
});
var result = results.join("");
if (result.length > 0) {
return result;
}
return n.toString();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment