Last active
September 25, 2022 19:33
-
-
Save ChrisWhealy/5897fa9585658f491e88fa1f681e56b9 to your computer and use it in GitHub Desktop.
Imperative implementation of the Fibonacci function
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
// Calculate the nth Fibonacci number: imperative style | |
function fibonacci(n) { | |
var a = 0, b = 1, sum = 0; | |
if (n<0) return NaN; | |
if (n<2) return n; | |
while (n>1) { | |
sum = a + b; | |
a = b; | |
b = sum; | |
n = n - 1; | |
} | |
return sum; | |
} |
Thanks - fixed now
Thank your for your effort
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
fibonacci(1) will return 0, which is not true