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

274
Views
what is the value of results for the makeCounter()

What is the value of const result? my guess was (3, 1). This is confusing to me so any help would be appreciated.

    function makeCounter() {
      let count = 0;
      return function() {
        count += 1;
        return count;
      }
    }
    const counter1 = makeCounter();
    const counter2 = makeCounter();
    counter1();
    counter1();
    
    const c1 = counter1();
    const c2 = counter2();
    const result = [c1,c2];
    console.log(result)

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Yes, [3, 1] is correct. (You can select 'run code snippit' to see it in action).

The reason here is that calling makeCounter() returns a function, with a local count variable initialized as zero.

Each time you call the the function returned by makeCounter() (i.e. counter1, or counter2), that counter's count is incremented.

counter1() is called 3 times, and counter2() is called once. And they are put together in an array at the end, thus [3, 1].

about 4 years ago · Juan Pablo Isaza Report
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!