Leo’s Fibonacci:
The Fibonacci sequence first appears in the book Liber Abaci (1202) by Leonardo of Pisa, Leonardo Pisano Bigollo, Leonardo of Pisa, Leonardo Bonacci, Leonardo Fibonacci or more know as Fibonacci.
DEMO:
http://leolanese.com/TDD/JsTestDriver/SpecRunner.Fibonacci.html
The rabbit question:
Rabbits are able to mate at the age of one month so that at the end of its second month a female can produce another pair of rabbits; rabbits never die and a mating pair always produces one new pair (one male, one female) every month from the second month on. The puzzle that Fibonacci posed was: how many pairs will there be in one year?
Fibonacci sequence:
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987,…
In mathematics, the Fibonacci numbers or Fibonacci series or Fibonacci sequence are the numbers in the following integer sequence:
By definition, the first two numbers in the Fibonacci sequence are 0 and 1, and each subsequent number is the sum of the previous two.
Automatic testing Fibonacci:
I created a simple approach using OOP JS inheritance about how to use Jasmine Unit-testing framework. In this case we’re testing the Leo’s Fibonacci number.
Final results runner:
Fibonacci.js
var Fibonacci = function() {};
Fibonacci.prototype.number = function(num){
var var1 = 0, var2 = 1, var3;var result = [];
var num = num;
result.push(var1);
result.push(var2);for(var i=3; i <= num; i++){
var3 = var1 + var2;
var1 = var2;
var2 = var3;
result.push(var3);
};return result;
};
var fibo = new Fibonacci();
// var num = prompt(“Enter the limit to fibonacci number: “, 10);
var num = 10;
fibo.number(num);
FibonacciSpec.js
describe(‘Fibonacci’, function(){
var num = 10;beforeEach(function() {
this.addMatchers({toBeDivisibleByTwo: function() {
return (this.actual % 2) === 0;
},
toBeDigit: function(){
String.prototype.isNumber = function(){
return /^\d+$/.test(this);
};
}});
});it(“Starting the Fibonacci number”, function() {
expect(Fibonacci.prototype.number(num)).toContain(0,1,1);
});it(‘Number based on num repetitions’, function(){
expect(Fibonacci.prototype.number(num)).toEqual([ 0, 1, 1, 2, 3, 5, 8, 13, 21, 34 ]);
});it(‘Numbers are Integer’, function() {
for(var i=0; i <= num; i++){
expect(parseInt(Fibonacci.prototype.number(num)[i]));
}
});it(“Fibonacci function has been called”, function() {
var fakeFibonacci = new Fibonacci();
spyOn(fakeFibonacci, ‘number’);fakeFibonacci.number(num);
// fakeFibonacci.number
expect(fakeFibonacci.number).toHaveBeenCalled();
expect(fakeFibonacci.number).toHaveBeenCalledWith(num);});
it(‘Fibonacci number validated’, function() {
var num = num -1;
var fibo = new Fibonacci();for(var i=0; i <= num; i++){
expect(fibo.number(num)[i]+fibo.number(num)[i+1]).toEqual(fibo.number(num)[i+2]);
}
});
});
Enjoy it !