How does this work?
Hello, I have a little logic problem in my head. I was trying to understand how does this code work:
It is from a lesson. What I am trying to find out is that: we ave defined a function with parameters base and exponent. Now it returns 0 if exponent is 1. In other cases it returns base * base^(exponent-1) which is basically x*x to the power of exponent -1.Code:var power = function (base, exponent){ if (exponent === 0){ return 1; } else { return base * power(base, exponent-1); } }; power(2,2);
Now how does base*power(base,exponent-1) work because I have not defined any way in this code to calculate it. As in how does the code know that I am trying to multiply x*x*x...etc.
I do not know if this makes any sense to any of you but if someone could explain.


Reply With Quote
Bookmarks