Last active
July 16, 2017 15:03
-
-
Save t301000/c7f1bed56434a3ea21f4b2c6fc1e331a to your computer and use it in GitHub Desktop.
請完成以下 API 設計,讓你的程式可以執行以下程式碼,並且得到預期的結果。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
請完成以下 API 設計,讓你的程式可以執行以下程式碼,並且得到預期的結果。串接 add 的次數沒有上限,最後一個 API 是 result() 才會回傳結果。 | |
$(1).add(2).add(3).add(99).result() === 105 | |
$(1).add(2).add(3).result() === 6 | |
etc... | |
...etc... | |
*/ | |
function $(start) { | |
var sum = start; | |
var obj = { | |
add: add, | |
result: result | |
} | |
function result() { | |
return sum; | |
} | |
function add(v) { | |
sum += v; | |
return obj; | |
} | |
return obj; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment