Skip to content

Instantly share code, notes, and snippets.

@chrisdothtml
Last active June 15, 2018 02:03
Show Gist options
  • Save chrisdothtml/9316c8a70ab8b5e80f948f45f241bbe0 to your computer and use it in GitHub Desktop.
Save chrisdothtml/9316c8a70ab8b5e80f948f45f241bbe0 to your computer and use it in GitHub Desktop.
Generate fibonacci sequence
/**
* 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