JavaScript Challenge - Fibonacci Finder

This was my go at it.

const fibFinder = n => { let fibArr = [0, 1]; for(let i = 2; i <= n; i++){ fibArr.push(fibArr[i-2] + fibArr[i-1]); } return fibArr[n]; } console.log(fibFinder(6)) // Leave this so that we can test your code: module.exports = fibFinder;
function fibFinder(n) {
  // Write your code here
  let count = 1
  let first = 0
  let second = n === 0 ? 0 : 1
  while (count < n) {
    count ++
    let newNum = first + second
    first = second
    second = newNum
  }
  return second
}

console.log(fibFinder(0))

// Leave this so that we can test your code:
module.exports = fibFinder;

function fibFinder(n) {
// Write your code here
let num1 = 0;
let num2 = 1;
let result = n;

for(let i = 2; i <= n; i++){
result = num1 + num2;
num1 = num2;
num2 = result;
}
return result;
}
console.log(fibFinder(6))

// Leave this so that we can test your code:
module.exports = fibFinder;

The only correct way to do this.

const fibFinder = (n) => Array(n).fill(0).reduce((a, _, i) => a.push(a[i] + a[i + 1]) && a, [0, 1])[n];

That is an indefensible statement and could not be farther from the truth.

2 Likes

While I enjoy condensing functions into a single line of code just to see if it’s possible, very seldom are one-liners the most efficient in terms of time or space complexity.

2 Likes
function fibFinder(n) { // Write your code here // Formula: Fn = (Fn - 1) + (Fn - 2) let fibNumbers = new Array(n); //Seed fibNumbers array fibNumbers[0] = 0; fibNumbers[1] = 1; for (let i = 2; i <= n; i++) { fibNumbers[i] = fibNumbers[i - 1] + fibNumbers[i - 2]; } return fibNumbers[n]; } console.log(fibFinder(6)) // Leave this so that we can test your code: module.exports = fibFinder;

Do we want a list of Fibonacci numbers, or do we want an answer to the question, what is the Nth term in the sequence? Think on this. What solution code is the best fit?

If the Nth term is what we are after then it makes sense to just run out the sequence to that term and call it a day. There will be very little memory use and we will have our answer.

Of course we can bring out the root five solution, but that’s paper not computer. Let’s not blur the lines. Still, I love paper and pencil.

1 Like

Hi, here is my solution

function fibFinder(n) {
  let a = 0;
  let b = 1;

  for(i = 0; i < n; i++) {
    [a, b] = [b, a + b];
  }
  
  return a;
}
1 Like

function fibFinder(n) {
if (n < 0) {
return ‘Index must be greater than or equal to zero’
} else if (n <= 1) {
return n
}
let arr = [0, 1];
while (arr.length < (n + 1)) {
arr[arr.length] = arr[arr.length - 1] + arr[arr.length -2]
}
return arr[n]
}

console.log(fibFinder(6))

// Leave this so that we can test your code:
module.exports = fibFinder;

  1. Recursion will keep a lot of memory in call stack until it release stack after end. More and more with any next step
  2. Recursion limited 5000-100000 deep levels depending on engine. Therefore, it is impossible to calculate, for example, 10000th Fibonacci number in Chrome browser. But it is not so important because fibFinder(1477) = Infinity
  3. This approach 5x+ slower than straightforward method with values swapping

This approach 5x slower than fastest

My like to your solution. It will be 3x+ faster with simple swapping.

function fibFinder(n) {
  let a = 0;
  let b = 1;
  let fib;
  for (let i = 0; i < n; i++) {
    fib = a + b;
    a = b;
    b = fib;
  }
  return a;
}