Fibonnacci
Write a recursive function called fib which accepts a number and returns the _n_th number in the Fibonacci sequence. Recall that the Fibonacci sequence is the sequence of whole numbers 1, 1, 2, 3, 5, 8, ... which starts with 1 and 1, and where every number thereafter is equal to the sum of the previous two numbers.
fib(4) // 3// This was my solution, and it works, but it's not elegant. But I solved it
// myself thinking through it and working through the knowledge
function fib(num) {
let total = 1;
let counter = num;
let prev = 0;
function helper(current, count) {
if(count === 1) return current;
total = prev + current;
prev = current;
return helper(total, count - 1);
}
helper(total, counter);
return total;
}
/**
For this method, I thought that it'd be the best idea to add an accumulator
Then I'd keep a "counter" that would be our baseline.
Then have a `prev` which will have a consistent carry for the current value
- Helper method
-> Using this I'd be able to have a base to return our "current"
-> Add to the total, with prev and current
-> Switch the prev to be what's current
-> And finally return the total and minus the count.
*/Last updated