Skip to content

Instantly share code, notes, and snippets.

@dtothefp
Created January 7, 2014 16:17
Show Gist options
  • Select an option

  • Save dtothefp/8301762 to your computer and use it in GitHub Desktop.

Select an option

Save dtothefp/8301762 to your computer and use it in GitHub Desktop.
// Loop Version
function loopFib(n){
var first = 0;
var second = 1;
var sum;
for(var i = 2; i <= n; i++){
sum = first + second;
first = second;
second = sum;
}
return sum;
}
// Recursive Version
function recFib(n){
if (n === 0){
return 0;
}
else if (n === 1){
return 1;
}
else {
return recFib(n-1) + recFib(n-2);
}
}
console.log(loopFib(5));
console.log(recFib(5));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment