If Mike could create a function, that then calls the function,that tells Mike to give himself one dollar, then Mike would have infinite wealth.
Here's how it works. Mike's function, instructs Mike to call his function, which gives himself one dollar. That call then calls Mikes function, which calls Mike's function, which calls Mike's function, which calls Mike's function, ..... infinity.
Okay.
function power(base, exponent) {
if (exponent == 0)
return 1;
else
return base * power(base, exponent - 1);
}
console.log(power(2, 3));
8 will be returned to the console.
How does this work?
It's pretty simple, but you might need to take a pencil and paper to understand it.
Step 1 : the function power is called in the console.log function. It's fed two arguments, 2 and 3. So the base is 2, and the exponent is 3. Exponent does not equal zero, so we move on to the else statement. The else statement says to return 2 times the value of calling the function power with arguments of 2 and 2.
Step 2: the function power is called as I said in the last line of step 1, with base equal to 2 and exponent equal to 2. So, exponent does not equal zero, so we move onto the else statement. The else statement says to return 2 times the value of calling the function power with arguments of 2 and 1.
Step 3 : the function power is called as I said in the last line of step 2, with base equal to 2 and exponent equal to 1. So, exponent does not equal zero, so we move onto the else statement. The else statement says to return 2 times the value of calling the function power with arguments of 2 and 0.
Step 4 : the function power is called as I said in the last line of step3, with base equal to 2 and exponent equal to 0. So, exponent does equal zero, so this particular function returns 1. Where does it return 1 to? It returns 1 to the function that called it, which is the function in step 3.
Step 5 : That means the function in step 3 now has a value for where it called the power function, it has the returned value of 1. So we get 2 times 1 which is then returned to the function that called it, which is the function in step 2.
Step 6 : So, the function in step 2 now has a value where it called the power function, and that value is 2. So the function in step 2 multiplies 2 by 2, which is 4, and returns that value to the function that called it, which is the function in step 1.
Step 7 : now we have a value where it says base * _______. The value sitting there is 4. 2 times 4 is 8. The value of 8 gets returned to the caller of power, which in this case is the console.log function, so 8 is printed to the console.
Step 1 : the function power is called in the console.log function. It's fed two arguments, 2 and 3. So the base is 2, and the exponent is 3. Exponent does not equal zero, so we move on to the else statement. The else statement says to return 2 times the value of calling the function power with arguments of 2 and 2.
Step 2: the function power is called as I said in the last line of step 1, with base equal to 2 and exponent equal to 2. So, exponent does not equal zero, so we move onto the else statement. The else statement says to return 2 times the value of calling the function power with arguments of 2 and 1.
Step 3 : the function power is called as I said in the last line of step 2, with base equal to 2 and exponent equal to 1. So, exponent does not equal zero, so we move onto the else statement. The else statement says to return 2 times the value of calling the function power with arguments of 2 and 0.
Step 4 : the function power is called as I said in the last line of step3, with base equal to 2 and exponent equal to 0. So, exponent does equal zero, so this particular function returns 1. Where does it return 1 to? It returns 1 to the function that called it, which is the function in step 3.
Step 5 : That means the function in step 3 now has a value for where it called the power function, it has the returned value of 1. So we get 2 times 1 which is then returned to the function that called it, which is the function in step 2.
Step 6 : So, the function in step 2 now has a value where it called the power function, and that value is 2. So the function in step 2 multiplies 2 by 2, which is 4, and returns that value to the function that called it, which is the function in step 1.
Step 7 : now we have a value where it says base * _______. The value sitting there is 4. 2 times 4 is 8. The value of 8 gets returned to the caller of power, which in this case is the console.log function, so 8 is printed to the console.
No comments:
Post a Comment