I have a recursive function that I would like to hide as an implementation detail. Here is are the two functions that I have to start:
function f(n) {
return fIter(2,1,0,n);
}
function fIter(a,b,c,n) {
return n===0 ? c : fIter(a+2*b+3*c, a,b,n-1);
}
console.log(f(5))
What would be the suggested way to hide the implementation details? Two examples that came to mind were:
const f = n => (function fIterative(a, b, c, n) {
return (n===0) ? c : fIterative(a+2*b+3*c, a, b, n-1)
})(2,1,0,n);
console.log(f(5))
And:
function f(n) {
function fIter(a,b,c,n) {
return n===0 ? c : fIter(a+2*b+3*c, a,b,n-1);
}
return fIter(2,1,0,n);
}
console.log(f(5))