public
Created

A minor correction to Angus Croll's Fibonacci generator (as illustrative of the comma operator).

  • Download Gist
pedantic-fibonacci.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/**
* 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.

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.