Last active
March 19, 2017 13:46
-
-
Save bennycode/8f765a7079de5597c5df05f70bb4f668 to your computer and use it in GitHub Desktop.
Fibonacci numbers
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
// Fibonacci numbers | |
/* extended version */ | |
function f(n) { | |
if (n === 0) { | |
return 0; | |
} else if (n === 1) { | |
return 1; | |
} else { | |
return f(n - 1) + f(n - 2); | |
} | |
}; | |
/* short version */ | |
function f(n) { | |
if (n === 0) return 0; | |
return (n === 1) ? 1 : f(n - 1) + f(n - 2); | |
} | |
const fn = f(11); | |
console.log(fn); // 89 | |
/* Promise-based version */ | |
function getFibonacciNumber(n) { | |
return Promise.resolve().then(() => { | |
return f(n); | |
}); | |
} | |
getFibonacciNumber(11).then((result) => { | |
console.log(`Result: ${result}`); // "Result: 89" | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment