Last active
June 15, 2018 02:03
-
-
Save chrisdothtml/9316c8a70ab8b5e80f948f45f241bbe0 to your computer and use it in GitHub Desktop.
Generate fibonacci sequence
This file contains 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
/** | |
* Generate fibonacci sequence up to `maxNum` | |
* https://en.wikipedia.org/wiki/Fibonacci_number | |
* | |
* @param {number} maxNum | |
*/ | |
function fibonacci (maxNum) { | |
const result = [0, 1] | |
let newNum, index | |
for (index = 2;;index++) { | |
newNum = result[index - 2] + result[index - 1] | |
if (newNum <= maxNum) { | |
result.push(newNum) | |
} else { | |
break | |
} | |
} | |
return result | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment