2620. Counter (?) (https://leetcode.com/problems/counter)
Дано целое число n, верните функцию-счётчик. Эта функция-счётчик изначально возвращает n, а затем при каждом последующем вызове возвращает значение на 1 больше предыдущего (n, n + 1, n + 2 и т.д.).
function createCounter(n: number): () => number {
return function () {
return n++
}
}Example 1:
Input:
n = 10
["call","call","call"]
Output: [10,11,12]
Explanation:
counter() = 10 // The first time counter() is called, it returns n.
counter() = 11 // Returns 1 more than the previous time.
counter() = 12 // Returns 1 more than the previous time.
Example 2:
Input:
n = -2
["call","call","call","call","call"]
Output: [-2,-1,0,1,2]
Explanation: counter() initially returns -2. Then increases after each sebsequent call.