Skip to content

Instantly share code, notes, and snippets.

@alyssonbruno
Last active August 29, 2015 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alyssonbruno/1eafb2046f0cd66eaec8 to your computer and use it in GitHub Desktop.
Save alyssonbruno/1eafb2046f0cd66eaec8 to your computer and use it in GitHub Desktop.
My 2 cents
// global scope
(function(){
// outer scope
var fibonacci = function(){
my_return = "0, ";
current_number = 0;
minus_one_order = 0;
minus_two_order = 0;
max_order=12;
for(current_order = 1;current_order<=max_order;current_order+=1){
if(current_order===1){
current_number = 1;
}
else{
current_number = minus_one_order+minus_two_order;
}
minus_two_order = minus_one_order;
minus_one_order = current_number;
my_return += current_number.toString()+(current_order<max_order?', ':'');
}
return my_return;
}
console.info(fibonacci());
})();
// global scope
(function(nth){
// outter scope
var fibonacci = function(max_order){
max_order = max_order==null?12:max_order;
current_number = 0;
minus_one_order = 0;
minus_two_order = 0;
for(current_order = 1;current_order<=max_order;current_order+=1){
if(current_order===1){
current_number = 1;
}
else{
current_number = minus_one_order+minus_two_order;
}
minus_two_order = minus_one_order;
minus_one_order = current_number;
}
return current_number;
}
console.info(fibonacci(nth));
// the call below is going to have a result with the position
// 2x ahead of the one passed to nth
console.info(fibonacci(nth*2));
})(10);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment