Skip to content

Instantly share code, notes, and snippets.

@founddrama
Created April 13, 2011 10:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save founddrama/917349 to your computer and use it in GitHub Desktop.
Save founddrama/917349 to your computer and use it in GitHub Desktop.
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