# CoDEVIANT #26 — Fibonacci Fun

• Every subsequent number in the list is the sum of the last two numbers
• Example: [0, 1, 1, 2, 3, 5, 8, 14, …..]
`function getNthFib(n) { let counter = 2 let array = [0, 1] while(counter < n) {  array.push(array[array.length - 2] + array[array.length - 1])  counter++} return n === 1  ?  0  : n === 2   ?  1  : array[n-2] + array[n-3]}`
• Death
• Taxes
• that any Fibonacci Sequence will start with 0 and then 1
`let counter = 2let array = [0, 1]`
`while(counter < n) {  array.push(array[array.length - 2] + array[array.length - 1])  counter++}`
`function getNthFib(n) { const lastTwo = [0,1] let counter = 3 while(counter <= n) {  const nextFib = lastTwo[0] + lastTwo[1]  lastTwo[0] = lastTwo[1]  lastTwo[1] = nextFib  counter++ } return n > 1 ? lastTwo[1] : lastTwo[0];}`
• Starting an array with 0 and 1
• Using a counter variable which gets incremented through a while-loop
• Using a ternary operator at the end to determine what will be returned
`while(counter <= n) {  const nextFib = lastTwo[0] + lastTwo[1]  lastTwo[0] = lastTwo[1]  lastTwo[1] = nextFib  counter++ }`
`return n > 1 ? lastTwo[1] : lastTwo[0]`

--

--

--

is a web developer, opera singer, actor, and lover of cats. (adrian-rosales.tech)

Love podcasts or audiobooks? Learn on the go with our new app.