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)
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].