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.
Ask or answer a question about this exercise by clicking reply () below!
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!
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) {
// Write your code here
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 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));
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;
// 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
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;
function fibFinder(n) {
// Write your code here
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;