Skip to content

@founddrama /pedantic-fibonacci.js
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
A minor correction to Angus Croll's Fibonacci generator (as illustrative of the comma operator).
/**
* Apologies to Angus Croll; re:
* http://javascriptweblog.wordpress.com/2011/04/04/the-javascript-comma-operator/
*/
// Yes, I am being a pedantic jerk, but:
var r = [1], n = 0, a = 0, b, next;
function nextFibonacci(){
next = a + (b = r[r.length - 1]);
return b = (a = b, next);
}
while(n++ < 10) {
r.push(nextFibonacci());
}
print(r); //[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
// ..._technically_ that's a Fibonacci sequence.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.