You are given a challenge to create a function named createHelloWorld
. This function should not take any explicit arguments but must return another function. The function that is returned should, when invoked, always return the string "Hello World", irrespective of any arguments passed to it.
Consider these examples to clarify the requirements:
If you call createHelloWorld
and then invoke the returned function without any arguments like this:
const f = createHelloWorld();
f(); // Expected output: "Hello World"
It should return "Hello World".
If you call createHelloWorld
and invoke the returned function with any number of arbitrary arguments, like this:
const f = createHelloWorld();
f({}, null, 42); // Expected output: "Hello World"
It should still return "Hello World".
Your task is to implement the createHelloWorld
function in JavaScript such that it adheres to these conditions. The goal is to create a higher-order function that encapsulates the behavior of returning a fixed string, regardless of the input it receives.
A naive solution is simply to define a function that returns another function. The inner function, when called, will always return the string "Hello World". This approach directly implements the problem description without any complex logic.
function createHelloWorld() {
return function() {
return "Hello World";
}
}
createHelloWorld
function and the returned function both execute in constant time, regardless of the input.The naive solution is already optimal since the problem requires a function that always returns the same string. No further optimization is needed.
function createHelloWorld() {
return function() {
return "Hello World";
}
}
createHelloWorld
function and the returned function both execute in constant time, regardless of the input.