Create once(func) that invokes func at most once and caches result. Subsequent calls return first result.
Skip to solutionKEEP THE
easyFrontend
Implement once(func) — ensure a function runs only once
765 views
01
Understand the problem
onceclosure
02
Attempt it yourself
Sketch your approach before reading the solution — that's what interviews test.
Nudge consolestandby
Stuck? Beam a request up — the console returns a conceptual nudge that guides your logic without spoiling the implementation.
03
Study the solution
Approach: Flag + cached result. Preserve this and args on first call.
function once(func) {
let called = false, result;
return function(...args) {
if (!called) { called = true; result = func.apply(this, args); }
return result;
};
}
// Demo
let n = 0;
const init = once(() => ++n);
con
Solution ready — 2 min read
Classified // press E to declassify
04
Join the discussion
Discussion (0)
Sign in to join the discussion.
No responses yet. Be the first to share what you think.
Transmission complete // awaiting log
KEEP THE
STREAK ALIVE.
Dossier 46 of 190 decoded in the JavaScript Coding track. One more won't hurt.