Skip to content

Instantly share code, notes, and snippets.

@bennycode
Last active March 19, 2017 13:46
Show Gist options
  • Save bennycode/8f765a7079de5597c5df05f70bb4f668 to your computer and use it in GitHub Desktop.
Save bennycode/8f765a7079de5597c5df05f70bb4f668 to your computer and use it in GitHub Desktop.
Fibonacci numbers
// 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