Closures & Functions

Function Once Wrapper

easyJS SpecificJS / TS3 tests

Problem Statement

Implement a function `once(fn)` that ensures `fn` can only be called once. Subsequent invocations return the value returned by the first call, without executing `fn` again.

Examples

Example 1:
let counter = 0;
const init = once(() => ++counter);
init(); // 1
init(); // 1
console.log(counter); // 1
JavaScript & TypeScript starter code included
Run this as a live interview

Practice solo

3 test cases. No second person needed.

Get new interview exercises

Practice problems and review drills, sent when we publish new ones. No spam.

Unsubscribe anytime. We never share your address.