Fibonacci loop
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
function fibonacci(num){ | |
var a = 1, b = 0, temp; | |
while (num >= 0){ | |
temp = a; | |
a = a + b; | |
b = temp; | |
num--; | |
} | |
return b; | |
} |
This version doesn't require a temp variable
function fibonacci(num) {
var i = 1, j = 0
while (num >= 1) {
j = i + j
i = j - i
num--
console.log(j)
}
//return (j)
}
fibonacci(10)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@shubich I was wondering the same thing. I guess if you change line 4 to "while (num > 0){" it will be correct.