Create Hello World Function - Problem

Write a function createHelloWorld that returns a new function that always returns "Hello World".

The returned function should ignore any arguments passed to it and consistently return the string "Hello World".

This problem demonstrates JavaScript concepts like closures and function factories.

Input & Output

Example 1 — Basic Function Creation
$ Input: f = createHelloWorld()
Output: "Hello World"
💡 Note: createHelloWorld() returns a function that always returns "Hello World" when called, regardless of arguments
Example 2 — Function with Arguments
$ Input: f = createHelloWorld(); f({}, null, 42)
Output: "Hello World"
💡 Note: Even when called with arguments, the returned function ignores them and returns "Hello World"
Example 3 — Multiple Calls
$ Input: f = createHelloWorld(); [f(), f(), f()]
Output: ["Hello World", "Hello World", "Hello World"]
💡 Note: The function consistently returns the same string on every call

Constraints

  • The function must return exactly "Hello World"
  • Arguments passed to returned function should be ignored
  • Function should work consistently across multiple calls

Visualization

Tap to expand
INPUTALGORITHMRESULTcreateHelloWorld()Function FactoryReturns new functionTask: Create function thatalways returns greeting1Define outer functioncreateHelloWorld()2Create inner functionIgnores all parameters3Return constant string"Hello World"4Return inner functionFrom outer functionFunction Output"Hello World"Always consistentIgnores argumentsSame every callcreatesoutputs💡Key Insight:Functions in JavaScript can return other functions (closures). The inner functionremembers its purpose and ignores any arguments passed to it.TutorialsPoint - Create Hello World Function | Closure Implementation
Asked in
Meta 25 Google 20 Amazon 15
89.2K Views
High Frequency
~5 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen