Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Zolbayar Orshil
9,838 PointsCan someone help me to explain why and how this function is working pls? I am reading it from book. recursion function
function power(base, exponent) {
if (exponent == 0)
return 1;
else
return base * power(base, exponent - 1);
}
console.log(power(2, 3));
// → 8
Can't understand how it is working ?
4 Answers
Thomas Nilsen
14,957 PointsHere is your example with comments, hope this helps:
function power(base, exponent) {
if (exponent == 0) return 1;
else return base * power(base, exponent - 1);
}
console.log(power(2, 3)); // → 8
//power(2, 3) => return 2 * power(2, 2)
//power(2, 2) => return 2 * power(2, 1)
//power(2, 1) => return 2 * power(2, 0)
//power(2, 0) => 1
//Now that we know power(2, 0) = 1, we can go all the way back
//power(2, 1) => return 2 * 1
//power(2, 2) => return 2 * 2
//power(2, 3) => return 2 * 4
//8
hunorcsszr
20,792 PointsHey!
This is a recursive function. It means that it calls itself. I try to break it down for you.
function power(base, exponent)
It's the function definition. A function named "power" with two input arguments "base" and "exponent". I think it's clear.
if (exponent == 0)
return 1;
If the given second argument is the number 0, then the function returns with number 1, because raising a number to the power zero is one.
else
If the exponent is not zero, then we have to do something with the given arguments. In this case something is raising a number.
When we raise a number it looks like this:
base = 2
exponent = 3
This equals to 2 * 2 * 2 = 8.
Other case:
base = 2
exponent = 6
2 * 2 * 2 * 2 * 2 * 2 = 64.
So, when we return like this:
return base * power(base, exponent - 1);
It means that the power function calls itself with the given exponent -1. And runs with every exponent from the given number all the way down. So 2 * 2 * 2 in your example. First run: exponent = 3 Second run: exponent = 2 Third run: exponent = 1
I hope this was clear. But this is another example. Factorial recursive function. It works just like your example. We test the input argument if it's less than 0 or 0. If not, the function will call itself with argument - 1;
function factorial(num)
{
// If the number is less than 0, reject it.
if (num < 0) {
return -1;
}
// If the number is 0, its factorial is 1.
else if (num == 0) {
return 1;
}
// Otherwise, call this recursive procedure again.
else {
return (num * factorial(num - 1));
}
}
console.log(factorial(5)); // 120
// So first num = 5, second = 4, third = 3, fourth = 2
function haveAGreatDay(count) {
if (count == 0) {
return "Have a great day, Zolbayar"
} else {
console.log(count);
return haveAGreatDay(count -1);}
}
// Try it out ;)
Leslie Heinzen
26,662 PointsThomas and hunorcsszr have already given lovely explanations of what is happening in the process of the function. Recursion is an important programming technique. Here is a good write-up on it.
An alternative to recursion for this type of function is an iterative solution, like this:
function power(base, exponent) {
var result = 1;
for (var i = 0; i < exponent; i++)
result = result * base;
return result;
}
It's debatable which version is more efficient, but the recursive solution is certainly more beautiful and demonstrates a good understanding of functional programming. For this particular example, however, there exists the Math.pow() function that is already a part of JavaScript's standard library.
Zolbayar Orshil
9,838 PointsThank you so much guys for nice explanations