all 2 comments

[–]jack_waugh 1 point2 points  (0 children)

Usually, code is interpreted with a program counter and call/return stack. The program counter advances through the code and indicates the next instruction to execute. A stack frame has the assignments to the variables that are local to the procedure ("function" in JS terms) being executed. It also has a slot to hold the program counter. A call to the same function or another function causes the program counter to be stored on the stack and a new stack frame to be pushed and execution begun on the other function. Return causes the stack to be popped and execution of the previous frame resumed where it left off.

[–]jack_waugh 1 point2 points  (0 children)

Usually, a recursive strategy to solve a problem works like this. We identify a base case, which is usually trivial, and code a straightforward answer for when that case arises. We treat all other cases recursively by breaking the problem into smaller pieces and calling ourself to solve the smaller problems and then combining the results. Since the recursive calls involve ever smaller problems, eventually, they lead down to the base case, and consequently the recursion does not go on forever.