1

Please help me understand this recursive function...

var stack = Array;
function power(base, exponent){
    if ( exponent === 0 ) {
        return 1;
    } else {
        stack[exponent-1] = base * power(base, exponent - 1);
        return stack[exponent-1];
    }
}

I dont understand what

stack[exponent-1]
is doing

2
  • 1
    shortest would be "base ^ exponent" :) Commented Apr 3, 2012 at 11:27
  • (To comment above: actually, ^ is exclusive or, not power.) Commented Mar 18, 2013 at 21:58

4 Answers 4

2

Which one? It's called twice. But each time it's either getting the value that exists in the array at the index equal to the current value of exponent-1 or setting that value.

It's just an array index and access.

Sign up to request clarification or add additional context in comments.

1 Comment

@Sam Huh? Explain how to access values that are members of an array? That's all that it is doing. Let's say exponent = 5, so that call translates into stack[4], accessing the fifth element of the array (array indexes start at 0). Mozilla documentation for array. They're both identical in nature, just one of them sets a value (the one with the =) and one of them gets a value.
1

I did console log of stack[exponent-1] using

var stack = Array;
function power(base, exponent){
    if ( exponent === 0 ) {
        return 1;
    } else {
        stack[exponent-1] = base * power(base, exponent - 1);
        console.log(stack[exponent-1]);return stack[exponent-1];
    }
}

O/P:

power(2,5)
2
4
8
16
32

So function class recursively until exponent become 0 (nth call), then it will start returning results

first it will return 1     (because exponent is 0)
        then returns 2 * 1 (return of n call)
              then   2 * 2 (return of n-1 call) 
              then   2 * 4 (return of n-2 call) and so on

2 Comments

Ohh, so it runs completely and then returns. I was wondering how it would be correct of it just kept returning! Thanks so much
when you call a function 1 and function 1 calls function 2 and function 2 called function 3, response will come first from function 3 then function 2 then function 1, its a stack en.wikipedia.org/wiki/Stack_(data_structure)
1

The algorithm is stacking the result of each power from the initial exponent to 0.

If you run power(2, 3), the stack will, at some point, be:

stack[2] = 8
stack[1] = 4
stack[0] = 2

This really has nothing to do with the mathematic concept of power.

3 Comments

I think that would be the other way around. stack[0] being 2 and stack[2] being 8.
How does it return all of them at once? That's the part confusing me to make the stack array complete.
That's exactly my point. You shouldn't return that statement. It's wrong.
1

have a look on this version!

function pow(x,n)
{
     return n==0?1:n==1?x:n==2?x*x:pow(pow(x,(n-n%2)/2),2)*(n%2==0?1:x);
}

can get it shorter?

1 Comment

Is this an answer, or a question? If it's a question, please post it as such. If it's an answer, I can't see how it answers the question. You could post the question on Code Review or Code Golf (as a challenge), perhaps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.