The task is to implement a count function that returns how many times it is called.
Create the function and initialise the variable that records how many times the function is called.
function counter () {
let calls = 0;
}
Implement the count function
function count(): number {
calls++;
return calls
}
A function to reset the number of calls is also needed
count.reset = () => {
calls = 0;
}
The number of counts should be returned
return count
There should be a function that calls the counter
const count = counter();
The final code
function counter () {
let calls = 0;
function count(): number {
calls++;
return calls;
}
count.reset = () => {
calls = 0;
}
return count;
}
const count = counter()
That's all folks!
Top comments (0)