# CoDEVIANT #26 — Fibonacci Fun

• You always start with 0, followed by 1.
• 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 + lastTwo  lastTwo = lastTwo  lastTwo = nextFib  counter++ } return n > 1 ? lastTwo : lastTwo;}`
• 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 + lastTwo  lastTwo = lastTwo  lastTwo = nextFib  counter++ }`
`return n > 1 ? lastTwo : lastTwo`

--

--