Created
April 13, 2011 10:49
-
-
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).
This file contains hidden or 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
/** | |
* 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