Skip to content

Instantly share code, notes, and snippets.

@zzarcon
Created Feb 26, 2016
Embed
What would you like to do?
Fibonacci loop
function fibonacci(num){
var a = 1, b = 0, temp;
while (num >= 0){
temp = a;
a = a + b;
b = temp;
num--;
}
return b;
}
@jason-mcdermott
Copy link

jason-mcdermott commented Sep 15, 2018

@shubich I was wondering the same thing. I guess if you change line 4 to "while (num > 0){" it will be correct.

@DrEVILish
Copy link

DrEVILish commented Jul 26, 2019

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