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內容數
留言0
查看全部
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 追蹤
Thumbnail
在創作的路上真的很多人問我說 到底要怎麼做出符合自己期待 但又可以表現得很有美感的作品?🥹 這個問題真的應該是每個創作者都一直在學習的課題吧!
提問的內容越是清晰,強者、聰明人越能在短時間內做判斷、給出精準的建議,他們會對你產生「好印象」,認定你是「積極」的人,有機會、好人脈會不自覺地想引薦給你
Thumbnail
在創作的路上真的很多人問我說 到底要怎麼做出符合自己期待 但又可以表現得很有美感的作品?🥹 這個問題真的應該是每個創作者都一直在學習的課題吧!
提問的內容越是清晰,強者、聰明人越能在短時間內做判斷、給出精準的建議,他們會對你產生「好印象」,認定你是「積極」的人,有機會、好人脈會不自覺地想引薦給你