2667. Create Hello World Function

Create Hello World Function

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"

The function returned by createHelloWorld should always return "Hello World".

 

Example 2:

Input: args = [{},null,42]
Output: "Hello World"
Explanation:
const f = createHelloWorld();
f({}, null, 42); // "Hello World"

Any arguments could be passed to the function but it should still always return "Hello World".

 

Constraints:

0 <= args.length <= 10

 

解法:

const createHelloWorld = () => (...args) => 'Hello World';

/**
 * const f = createHelloWorld();
 * f(); // "Hello World"
 */

Copyright © 2025 - All right reserved