Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

220
Views
How do recursive functions have access to info stored in a local memo object?

I'm trying to understand this memoized / recursive version of fibonacci -

 function fib(n, memo = {}) {
    if (n in memo) return memo[n];
    if (n === 1 || n === 2) return 1;

    memo[n] = fib(n - 1, memo) + fib(n - 2, memo); 
    return memo[n]
 }

What I don't understand is how the information saved to a local memo object from inner recursive calls / scopes can be accessed by outer scopes. I.e. if a function calls itself, and then saves some info to the memo, how can the memo object in the higher scope (from where the recursive call was made) be updated if the update was made in a different scope?

When calling a function inside of another function, the former function doesn't have access to the latter function's local variables. When a function is called recursively, I like to think of it as though a function is calling a completely different function. This way of thinking has been helpful, but perhaps in this case it's misleading / wrong.

Thanks for the help in advance frens!

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!