From 74463ba6ff17b0ed9efc76583375cfdfc881b066 Mon Sep 17 00:00:00 2001 From: animalaes Date: Thu, 31 Mar 2011 18:10:24 +0200 Subject: [PATCH] Kata marzo realiza con Javascript y Jasmine --- animalaes/fizzbuzz.html | 23 +++++++++++++++++++++ animalaes/fizzbuzz.js | 33 ++++++++++++++++++++++++++++++ animalaes/fizzbuzzSpec.js | 42 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 98 insertions(+) create mode 100644 animalaes/fizzbuzz.html create mode 100644 animalaes/fizzbuzz.js create mode 100644 animalaes/fizzbuzzSpec.js diff --git a/animalaes/fizzbuzz.html b/animalaes/fizzbuzz.html new file mode 100644 index 0000000..4a361fb --- /dev/null +++ b/animalaes/fizzbuzz.html @@ -0,0 +1,23 @@ + + + + Jasmine Test Runner + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/animalaes/fizzbuzz.js b/animalaes/fizzbuzz.js new file mode 100644 index 0000000..0c5493d --- /dev/null +++ b/animalaes/fizzbuzz.js @@ -0,0 +1,33 @@ +function FizzBuzz() { +}; + +FizzBuzz.prototype.fizz = "Fizz"; + +FizzBuzz.prototype.buzz = "Buzz"; + +FizzBuzz.prototype.say = function(number) { + var result = ""; + if (this.isMultipleOfThree(number) || this.containsTree(number)) + result += this.fizz; + if (this.isMultipleOfFive(number) || this.containsFive(number)) + result += this.buzz; + if (result.length == 0) + result += number; + return result; +}; + +FizzBuzz.prototype.isMultipleOfThree = function(number) { + return number % 3 === 0; +}; + +FizzBuzz.prototype.isMultipleOfFive = function(number) { + return number % 5 === 0; +}; + +FizzBuzz.prototype.containsTree = function(number) { + return number.toString().indexOf('3') != -1; +}; + +FizzBuzz.prototype.containsFive = function(number) { + return number.toString().indexOf('5') != -1; +}; \ No newline at end of file diff --git a/animalaes/fizzbuzzSpec.js b/animalaes/fizzbuzzSpec.js new file mode 100644 index 0000000..c4925ff --- /dev/null +++ b/animalaes/fizzbuzzSpec.js @@ -0,0 +1,42 @@ +describe("FizzBuzz", function() { + var fizzBuzz; + + beforeEach(function() { + fizzBuzz = new FizzBuzz(); + + }); + + it('should return number', function() { + expect(fizzBuzz.say(2)).toEqual('2'); + expect(fizzBuzz.say(4)).toEqual('4'); + expect(fizzBuzz.say(7)).toEqual('7'); + expect(fizzBuzz.say(19)).toEqual('19'); + }); + + it('should return Fizz', function() { + expect(fizzBuzz.say(3)).toEqual('Fizz'); + expect(fizzBuzz.say(13)).toEqual('Fizz'); + expect(fizzBuzz.say(18)).toEqual('Fizz'); + expect(fizzBuzz.say(96)).toEqual('Fizz'); + }); + + it('should return Buzz', function() { + expect(fizzBuzz.say(5)).toEqual('Buzz'); + expect(fizzBuzz.say(20)).toEqual('Buzz'); + expect(fizzBuzz.say(95)).toEqual('Buzz'); + expect(fizzBuzz.say(100)).toEqual('Buzz'); + }); + + it('should return FizzBuzz', function() { + expect(fizzBuzz.say(15)).toEqual('FizzBuzz'); + expect(fizzBuzz.say(35)).toEqual('FizzBuzz'); + expect(fizzBuzz.say(54)).toEqual('FizzBuzz'); + expect(fizzBuzz.say(75)).toEqual('FizzBuzz'); + expect(fizzBuzz.say(90)).toEqual('FizzBuzz'); + }); + + for ( var x = 0; x < 101; x++) { + console.log(fizzBuzz.say(x)); + } + +}); \ No newline at end of file