2665. Counter II

更新於 發佈於 閱讀時間約 2 分鐘

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;
};
avatar-img
0會員
5內容數
留言
avatar-img
留言分享你的想法!

































































高的沙龍 的其他內容
(略),array 是用來將陣列的值進行累加,我們來看看怎麼怎麼達成吧: Given an integer array nums, a reducer function fn, and an initial value init, return the final result obtained
問題 Write a function expect that helps developers test their code. It should take in any value val and return an object with the following two functio
(略),array 是用來將陣列的值進行累加,我們來看看怎麼怎麼達成吧: Given an integer array nums, a reducer function fn, and an initial value init, return the final result obtained
問題 Write a function expect that helps developers test their code. It should take in any value val and return an object with the following two functio
你可能也想看
Google News 追蹤