Create Hello World Function - Problem
Write a function createHelloWorld. It should return a new function that always returns "Hello World".
This problem introduces the concept of closures in JavaScript - functions that have access to the outer (enclosing) scope's variables even after the outer function has returned.
Input & Output
Example 1 — Basic Function Creation
$
Input:
createHelloWorld()
›
Output:
function that returns "Hello World"
💡 Note:
createHelloWorld() returns a new function. When that returned function is called, it outputs "Hello World"
Example 2 — Multiple Calls
$
Input:
const f = createHelloWorld(); f(); f();
›
Output:
"Hello World", "Hello World"
💡 Note:
The same returned function can be called multiple times, always returning the same string
Example 3 — Multiple Instances
$
Input:
const f1 = createHelloWorld(); const f2 = createHelloWorld();
›
Output:
Two independent functions, both return "Hello World"
💡 Note:
Each call to createHelloWorld() creates a new function instance, but they all behave identically
Constraints
- The returned function should always return the exact string "Hello World"
- No parameters are passed to createHelloWorld or the returned function
Visualization
Tap to expand
Understanding the Visualization
1
Input
Call createHelloWorld() with no parameters
2
Process
Create and return inner function
3
Output
Returned function always outputs "Hello World"
Key Takeaway
🎯 Key Insight: Functions can return other functions, creating flexible and reusable code patterns
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code