Skip to content

Commit 6263625

Browse files
committed
Add solution #2665
1 parent 4f675fb commit 6263625

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,7 @@
390390
2648|[Generate Fibonacci Sequence](./2648-generate-fibonacci-sequence.js)|Easy|
391391
2649|[Nested Array Generator](./2649-nested-array-generator.js)|Medium|
392392
2650|[Design Cancellable Function](./2650-design-cancellable-function.js)|Hard|
393+
2665|[Counter II](./2665-counter-ii.js)|Easy|
393394
2703|[Return Length of Arguments Passed](./2703-return-length-of-arguments-passed.js)|Easy|
394395
3110|[Score of a String](./3110-score-of-a-string.js)|Easy|
395396
3392|[Count Subarrays of Length Three With a Condition](./3392-count-subarrays-of-length-three-with-a-condition.js)|Easy|

solutions/2665-counter-ii.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* 2665. Counter II
3+
* https://leetcode.com/problems/counter-ii/
4+
* Difficulty: Easy
5+
*
6+
* Write a function createCounter. It should accept an initial integer init.
7+
* It should return an object with three functions.
8+
*
9+
* The three functions are:
10+
* - increment() increases the current value by 1 and then returns it.
11+
* - decrement() reduces the current value by 1 and then returns it.
12+
* - reset() sets the current value to init and then returns it.
13+
*/
14+
15+
/**
16+
* @param {integer} init
17+
* @return { increment: Function, decrement: Function, reset: Function }
18+
*/
19+
var createCounter = function(init) {
20+
let current = init;
21+
return {
22+
increment: () => ++current,
23+
decrement: () => --current,
24+
reset: () => current = init,
25+
};
26+
};

0 commit comments

Comments
 (0)