Created
September 7, 2011 12:21
-
-
Save cironunes/1200418 to your computer and use it in GitHub Desktop.
Fibonacci number script
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
var Fibonacci = function () { | |
//private | |
var nums = [0,1]; | |
return { | |
pushNumbers: function (q) { | |
//check if value pass to this method is a number | |
//if not set it to 0 | |
q = typeof q === 'number' ? q : 0; | |
for (var i = 0; i < q; i ++) { | |
//get actual size and calc next number to push in array | |
var actSize = nums.length - 1, | |
next = nums[actSize] + nums[actSize - 1]; | |
nums.push(next); | |
} | |
}, | |
getSeq: function () { | |
return nums; | |
} | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment