Write a function createCounter
. It should accept an initial integer init
. It should return an object with three functions.
The three functions are:
increment()
increases the current value by 1 and then returns it.decrement()
reduces the current value by 1 and then returns it.reset()
sets the current value to init
and then returns it.解題思路:
因為我一值卡在輸出的時候 init 沒辦法更新數字,會直接,後來才在提示看到要先加一個 count
程式碼如下:
const createCounter = function(init) {
let currentCount = init;
const counter = {
increment: function() {
currentCount++;
return currentCount;
},
decrement: function() {
currentCount--;
return currentCount;
},
reset: function() {
currentCount = init;
return currentCount;
}
};
return counter;
};