#problem about recursion

1 messages · Page 1 of 1 (latest)

wanton coyote
#
var minCostClimbingStairs = (cost, i = cost.length, memo = new Map()) => {
    // debugger;
    if (i <= 1) return 0;

    if (memo.has(i)) return memo.get(i);;

    const [ prev, prevPrev ] = [ (i - 1), (i - 2) ];
    const downOne = minCostClimbingStairs(cost, prev, memo) + cost[prev];   
    const downTwo = minCostClimbingStairs(cost, prevPrev, memo) + cost[prevPrev];

    memo.set(i, Math.min(downOne, downTwo));

    return memo.get(i);
}

console.log(minCostClimbingStairs([20, 10, 40, 5]));

when we hit base case it returns 0;

but the function returning 15 in the end that means the function can access index 1

[20, 10, 40, 5]

The result is 10 + 5

so that is 15

10 is in index 1 ..that's what confusing me

So I don't know how this recursion goes

viral fableBOT
#

<@&987246717831381062> please have a look, thanks.

viral fableBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.

Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.

wanton coyote
#

Yes

viral fableBOT
#

Java and JavaScript are two completely unrelated programming languages.
https://i.imgur.com/yYFdXet.png

wispy stratus
#

this is a java server

wanton coyote
#

Yeah currently I'm learning java.sorry My first language is javascript

wispy stratus
#

yeah the thing is you asking a javascript question on a java server, which might not be really helpful

#

but tbh your function doesnt seem to be that complex

#

so some might be able to help you

wanton coyote
#

I understand it.but the base case confusing me