Your function as shown will try to recurse infinitely, because the recursive call on the line with f + fE(f) passes the same value of f through each time, which means the if test on the first line will always fail.
It doesn't make any sense to implement this recursively, because you can calculate the correct result with one line of code:
function fE(f) { return 2 * f; }
Perhaps a better example of a recursive function (for learning purposes) would be a function that multiples any two numbers together via a larger number of recursive calls:
multiply(10, 3) // return 30
...again that could easily be implemented in one line:
function multiply(a, b) { return a * b; }
...but you could do it with recursion like this:
function multiply(a, b) {
console.log(a, b);
if (b === 0)
return 0;
else if (b === 1)
return a;
else if (b < 0)
return -multiply(a, -b);
else
return a + multiply(a, b-1);
}
multiply(12, 4) // 48
multiply(5, -3) // -15
multiply(3, 0) // 0
Notice that each time the function calls itself it passes a different value in the second argument, so that eventually the else if (b === 1) condition will be true and the recursion will stop. I've included a console.log(a,b) so that you can see for yourself what the arguments are on each call.
fargument through. (Plus it doesn't make sense to use recursion for this problem.)function fE(f) { return 2 * f; }f < 0)