# JavaScript Challenge - Fibonacci Finder

This community-built FAQ covers the “Fibonacci Finder” code challenge in JavaScript. You can find that challenge here, or pick any challenge you like from our list.

## Top Discussions on the JavaScript challenge Fibonacci Finder

There are currently no frequently asked questions or top answers associated with this challenge – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this challenge. Ask a question or post a solution by clicking reply () below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this challenge, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

## Join the Discussion. Help a fellow learner on their journey.

You can also find further discussion and get answers to your questions over in #get-help.

Agree with a comment or answer? Like () to up-vote the contribution!

Need broader help or resources? Head to #get-help and #community:tips-and-resources. If you are wanting feedback or inspiration for a project, check out #project.

Looking for motivation to keep learning? Join our wider discussions in #community

Found a bug? Report it online, or post in #community:Codecademy-Bug-Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

``````Hi all,
A simple solution. Members 0 and 1 are defined in advance as we need to iterate only for n>=2

function fibFinder(n) {
let fibarray = []
fibarray[0] = 0
fibarray[1] = 1
for(let i=2; i<=n; i++)
{
fibarray[i] = fibarray[i-1] + fibarray[i-2]
}
return fibarray[n]
}

console.log(fibFinder(6))

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

The Fibonacci sequence this is Constant-recursive sequence Constant-recursive sequence - Wikipedia. It means we can use old good recursive function (the function which is calling itself). Benefits of using this approach is that we no need to create an array and fill every element of it and as a result keep useless memory heap.

``````function fibFinder(n, n_2 = 0, n_1 = 1, inc = 2) {
if(n === 0) return 0;
if(n === 1) return 1;
let current = n_2 + n_1;
if(inc === n) return current;
return fibFinder(n, n_1, current, ++inc);
}

console.log(fibFinder(3))

// Leave this so that we can test your code:
module.exports = fibFinder;
``````
``````> function fibFinder(n) {
>   let fibs = [0,1]
>   if(n===0){
>     return 0} else{
>   for (let i = 1; i < n; i++){
>     let next = fibs[fibs.length-1]+fibs[fibs.length-2];
>     fibs.push(next);
>   }
>   return(fibs[fibs.length-1])
> }}
``````
``````function fibFinder(n) {

let fibArr = [0,1]
let sum;

for (let i = 1; i <= n ; i++) {
sum = fibArr[fibArr.length-1] + fibArr[fibArr.length-2]
console.log(sum, "what is up here")
fibArr.push(sum)
console.log(fibArr,"is the array updated?")
}
return fibArr[n]// Write your code here
}

console.log(fibFinder(5))

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

Keeping it in a non-growing 2 element array and using ES6 destructuring assignment to swap/reassign the values of fib[0] and fib[1] in one line.

``````function fibFinder(n) {
const fib = [0, 1]; // First nr located at fib[0]
for (let i = 0; i < n; i++) {
[fib[0], fib[1]] = [fib[1], fib[0] + fib[1]];
}
return fib[0]; // After updating the array n times we return fib[0]
}
``````

Hey, here’s my solution:
I store the first 2 element in variables and assign x = 1.
Then use destructuring assignment on them as arrays to make a = b and b = a + b.

function fibFinder(n) { let a = 0; let b = x = 1; while(x <= n){ [a, b] = [b, a + b]; n--; } return a; } console.log(fibFinder(6));

Non recursive solution here, but I think it’s short and to the point

``````function fibFinder(n) {
let fibNums = [0, 1]
for(let i=2; i<=n; i++){
if(fibNums.length <= n + 1) {
fibNums.push(fibNums[i - 1] + fibNums[i - 2])
} else if (fibNums.length > n) {
return fibNums[n]
}
}
return fibNums[n]
}
``````

not very beautiful solution, i found recursive solution most beautiful

``````function fibFinder(n) {
let x = 0;
let y, z;
y = z = 1;
if (n === 0) {
return 0;
}
while (n > 1) {
z = x + y;
x = y;
y = z;
n--;
}
return z;
}

console.log(fibFinder(6))

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

This approach uses a memoized function to speed up the calculation. By caching the values you can reduce the calculations needed over each iteration, useful when trying to find a large Fibonacci number.

function memoize(fn) { const cache = {}; return function(...args) { if (cache[args]) { return cache[args]; } const result = fn.apply(this, args); cache[args] = result; return result; }; } function fib(n) { if (n < 2) { return n; } return fibFinder(n - 1) + fib(n - 2); } const fibFinder = memoize(fib); console.log(fibFinder(6)) // Leave this so that we can test your code: module.exports = fibFinder;

On line 7 should it be `x` that gets decremented?

Was curious what would run faster…

``````// expanding array
function __fibFinder(n) {
let a = [];
for (i = 0; i <= n ; i++) {
a[i] = i > 1 ? a[i-1] + a[i-2] : i
}
return a[n];
}

// array of length 2
function _fibFinder(n) {
let a = [0, 1];
for (i = 0; i < n; i++) {
[a[0],a[1]] = [a[1], a[0] + a[1]];
}
return a[0];
}

// array of length 2 with no external variable
function fibFinder(n) {
return (arr => {
for (i = 0; i < n; i++) {
[arr[0],arr[1]] = [arr[1], arr[0] + arr[1]];
}
return arr[0]
})([0,1])
}

// timers
let __startTime = Date.now();
for (let i = 0; i < 1000; i++) __fibFinder(10000);
console.log(Date.now() - __startTime); // ~103ms

let _startTime = Date.now();
for (let i = 0; i < 1000; i++) _fibFinder(10000);
console.log(Date.now() - _startTime); // ~1170ms

let startTime = Date.now();
for (let i = 0; i < 1000; i++) fibFinder(10000);
console.log(Date.now() - startTime); // ~1329ms
``````

Here’s my simple solution

const fibFinder = n => { let fibs = [0, 1]; while (fibs.length <= n) { let z = fibs.length-1; let y = fibs.length-2; fibs.push(fibs[z]+fibs[y]) } return fibs[n]; } // Glory to you and your code console.log(fibFinder(0)) console.log(fibFinder(1)) console.log(fibFinder(2)) console.log(fibFinder(3)) console.log(fibFinder(4)) console.log(fibFinder(5)) console.log(fibFinder(6)) console.log(fibFinder(7)) console.log(fibFinder(8)) console.log(fibFinder(9)) console.log(fibFinder(10)) // Leave this so that we can test your code: module.exports = fibFinder;
``````function fibFinder(n) {
//const phi = (1 + Math.sqrt(5)) / 2;
//return ((phi**n - (-1/phi)**n) / Math.sqrt(5))
//F(n) = F(n-1) + F(n-2)
let fib = [];
for (let i = 0; i <= n; i++) {
let num;
if (i > 1) {
num = fib[i-1] + fib[i-2];
} else {
// 0, 1
num = i;
}
fib.push(num);
}
return fib.pop();
}

console.log(fibFinder(100))

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

I used a `switch` statement.

``````function fibFinder(n) {
switch (n) {
case 0:
return 0;
case 1:
return 1;
default:
return fibFinder(n - 2) + fibFinder(n - 1);
}
}

console.log(fibFinder(6))

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

I think this is the most basic non-creative solution…

function fibFinder(n) {
const fibArr=[0,1];
for (i=2;i<=n;i++){
fibArr.push(fibArr[i-2]+fibArr[i-1]);
}
console.log(fibArr);
return fibArr[n];
}

module.exports = fibFinder;

``````function fibFinder(n) {
return  n < 1 ? 0 :
n <= 2 ? 1 :
fibFinder(n-1) + fibFinder(n-2)
}

console.log(fibFinder(6))

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

function fibFinder(n) {
const fbArray = [0,1]

for (i=2; i<=n; i++) {
fbArray[i]=0
}
for (i=2; i<=n; i++ ) {
const a = fbArray[i-2]
const b = fbArray[i-1]
fbArray[i]=(a+b)
}
return fbArray[n]
}

console.log(fibFinder(6))

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

``````function fibFinder(n)
{
if (!n) return 0;
if (n == 1) return 1;
return fibFinder(n - 1) + fibFinder(n - 2);
}

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

Here’s a silly solution that uses the golden ratio to get the next term of the sequence.

``````const fibFinder = (n) => {
if (n <= 0) {
return 0;
}
else if (n <= 2) {
return 1;
}
const phi = 1.618033988749894; // golden ratio
let x = 1;
for (let i = 3; i <= n; i++) {
x = Math.round(x * phi);
}
return x;
}
``````