FrontendArk

Master Frontend Interviews

  • Practice
  • Playground
  • Resource
Easy

Singleton

10mins

Implement a function singleton(fn) that ensures the given function fn is called at most once.
Subsequent calls should return the same value returned by the first invocation.

Examples

let count = 0; const init = singleton(() => ++count); init(); // 1 init(); // 1 init(); // 1 // Even if called multiple times, only the first call executes the original function.

Code Editor

JavaScript Function

00:00
Loading...

Test Cases

No test results yet

Click "Run" to execute tests