-3

I found this function in another question but i don't really understand how it works.

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

    console.log(fibonacci(4));
    //fibonacci secuence
Nathan Hughes
  • 94,330
  • 19
  • 181
  • 276
  • Do you understand Recursion? Take a piece of paper out with a pen. Set n=4 and follow the code by hand. If you can't figure it out from there then comment here and I will show you – RSon1234 Oct 02 '17 at 03:08
  • Exactly what don't you understand about it? Please be more specific. – Spencer Wieczorek Oct 02 '17 at 03:10
  • Take a look at this answer for some help https://stackoverflow.com/questions/8845154/how-does-the-the-fibonacci-recursive-function-work but I agree with RSon best to take out a pen and paper and go through the algorithm yourself – Kartik Prasad Oct 02 '17 at 03:13
  • what is the question – Aniruddha Das Oct 02 '17 at 03:13
  • The function is summing the numbers generated for the ternary operators? – Jorge Bermúdez Oct 02 '17 at 04:13
  • i saw that recursive functions are a bit slow in comparison with a code like this function Fib(n) { var a = 0; var b = 1; var c = 0; while (a < n) { console.log(a + ""); c = a; a = b; b = a + c; } } Fib(10); – Jorge Bermúdez Oct 02 '17 at 04:16

2 Answers2

0

function fibonacci(n) {} is one of the way of writing function in javascript. This is called function Declaration.One of the advantage of declaring in this way is you can call the same function when there is recursion,mean in the function body you can call the function again using the name that is being done here fibonacci(n - 1) + fibonacci(n - 2); where fibonacci is the function name.

The following is a syntax for ternary operator

condition ? expr1 : expr2

Which mean is if condition is evaluated to true then expr1 will be evaluated & if false expr2 will be evaluated.

The ternary operator can be nested(not recommended as it increase function complexity)

n < 1 ? 0 : n <= 2 ? 1 : fibonacci(n - 1) + fibonacci(n - 2);

The above expression is nested ternary operator ,for simplicity of understanding it can be divided in two part ,part-1 n < 1 ? 0 ,part-2 : n <= 2 ? 1 : fibonacci(n - 1) + fibonacci(n - 2).Part-2 is nested ternary operation.

According to part-1 if n is less than 1, set it to 0 else(second part) if n is less than equal to 2 set it to 1, else again recall the same function by using the name of the function(fibonacci)

brk
  • 48,835
  • 10
  • 56
  • 78
  • Thanks a lot I think i expressed bad about asking of my doubts, i never though you'll asnwer me so fast wow you're awesome guys! – Jorge Bermúdez Oct 02 '17 at 04:12
0

Well that's not too though i think! According to me..Let me assume that u don't know about Fibonacci series. So Fibonacci series is Look like 0,1,1,2,3,5,8... as 1=1+0, 2=1+1 ,3=2+1, 5=3+2; means every next no is a sum of 2 previous numbers. Now lets talk about the code :
Fibonacci(n-1) + Fibonacci(n-2)statements return the sum of 2 previous numbers if no is greater than 1 and 2. as a example if Fibonacci(4); is call then is call againFibonacci(3)+Fibonacci(2); which call again Fibonacci(3)=(Fibonacci(2)+Fibonacci(1)=1+0=return "1";. And Fibonacci(2)="1";then result is=1+1=2. Now look at index 4 on series(line 2 above) its 2 on series 0,1,1,"2". Hope You Get Your answer..Thanks