# Different output problem in javascript

``````// function 1

function getNthFib(n) {
if (n == 1){
return 0;
}
else if(n == 2){
return 1;
}
else {
return (n-1)+(n-2)
}
}

// function 2

function getNthFib2(n) {
if (n == 1){
return 0;
}
else if(n == 2){
return 1;
}
else {
return getNthFib2(n-1)+getNthFib2(n-2)
}
}
console.log(getNthFib(3))  // output = 3
console.log(getNthFib2(3)) // output = 1
``````

Can anyone explain why the output of these two function are different ?

The above returns a number and stops further processing.

This returns the sum of recursive calls to the function. Big difference.

I can see the difference too. But I a not understanding the concept behind the second code. Kindly explain

With your example of 3 you have the following:
#1: is n == 1? no. is n == 2? So return (n-1)+(n-2)… return (3-1=2) + (3-2=1)… return 3.
#2: is n == 1? no. is n == 2? So return (getNthFib(n-1)+getNthFib(n-2))…
return (getNthFib(3-1=2)+getNthFib(3-2=1))… each of these evalute so you have
return ((getNthFib(2) = 1, because if n==2, return 1) + (getNthFib(1) = 0, because if n==1 return 0)…
return (1 + 0)… return 1