Now we're coming to some really hilarious questions, because this stuff you will never use in your
everyday life like a programmer, but you must know how to solve it.
And the first one here, which is my favorite design, the function which returns a Fibonacci sequence
value and they didn't write the function to get the Fibonacci value in the last 12 years, which actually
means it is not that needed.
But I am constantly hear this question again and again on the interview.
And actually, I think that it is totally fine to ask the interviewer, OK, I don't know, a formula
for Fibonacci.
Can you please say it and then they can code it?
It's not a problem.
And this is totally fine, because you should not know by heart.
All mathematical formulas.
This is what here.
Actually, you can get something like this as a result for your question.
So here you can see that Fibonacci sequence is an integer sequence where the first two terms is zero
and one after the next term is defined as a sum of previous two terms.
And the else term is the sum of and minus one and and minus two, which actually means this is a typical
start of the sequence we have here zero, one, one and two.
And as you can see here, we can sum, for example, one and one, and we get into here with some one
and two and we get them three and so on and so on.
So how we can solve this, we must provide in our function a Fibonacci sequence value, which actually
means the index in this sequence.
For example, if we will provide seven as an argument, then we must get here 21.
Because actually we will sum up eight and 13 and we're getting 21.
Now let's call this exercise and actually he'll be able to use recursion because if you want to calculate
its recursive loop.
So here we can create a Fibonacci function and we get here and which is our index.
And here, first of all, we must check if it is less than two, because in this case, our logic won't
work because here we have zero and one.
This is we have a master, OK, if and less than two, then he'll want to return one.
In other cases, we must apply our logic.
And here we simplify return Fibonacci.
And here we are passing and minus two, which actually means we calculate this function recursively.
And every single recursive call of Fibonacci will call it again and again until we will come to this
first condition.
So here we have Fibonacci and minus two plus Fibonacci and minus one.
And actually, this is a whole code that you must trade.
Let's check this out.
I will just jump in the console and right here, Fibonacci seven.
And as you can see, we're getting 21, which actually means our code is working.
But here are some important stuff to remember.
As a programmer, you must know how to convert mathematical formulas or some logic in JavaScript functions
or functions in any other language, but it's totally fine to not know beyond all mathematical formulas.
This is why you should not afraid to ask about them.
And secondly, I think it is totally fine to ask in return.
OK, I can't feel the code it, but are you using this stuff every single day in your project?
And probably you will get an answer.
No.
This is why actually, it doesn't make any sense to ask this stuff in the first place.