Write a function createHelloWorld. It should return a new function that always returns “Hello World”.
請寫一個 createHelloWorld 函式,且它應該始終返回 "Hello World" 的結果。
Example 1:
Input: args = [] Output: "Hello World" Explanation: const f = createHelloWorld(); f(); // "Hello World"
Example 2:
Input: args = [{},null,42]
Output: "Hello World"
Explanation:
const f = createHelloWorld();
f({}, null, 42); // "Hello World"
solution:
從範例1中可以看到,宣告了 f 為 createHelloWorld() 的這個 function,且範例2嘗試將符號、null、整數…等,放入 function 中。
然而我們可以看到官方在 createHelloWorld 的函式中,希望運用「…args」展開運算式,試圖將一些 input 放入 function 後進行回傳,而題目又希望只會回傳指定字串。
所以我們僅需確保這個 function 被呼叫之後,能夠回傳 “Hello World” 即可。
ex. args = arguments
Code 1: BigO(1)
var createHelloWorld = function() {
return function(...args) {
return "Hello World"
}
};
FlowChart:
Example 1
const f = createHelloWorld(); f(); // "Hello World"
Example 2
const f = createHelloWorld();
f({}, null, 42); // "Hello World"