I was doing some functional programming practice with javascript lambda expressions and came up with the following code. The code aims to return a function that calculates given number's power of given base value. My code as follows;
const findUpper = base => (num, i = 0) => {
if (num < base) {
return i;
} else {
i++;
return findUpper(Math.round(num / base), i);
}
}
const findUpper2 = findUpper(2)
console.log(findUpper2(8)) //expected to have 3
The problem here is, recursion becomes broken after first findUpper
call, because it returns a function.
How can I make this snippet work?