cf.completefrontend

Write the codeEasy

Write once(fn): a function that runs fn only the first time it's called.

once(fn) returns a new function. The first time it's called, it calls fn with the same this and arguments it received, and returns fn's result.

Later calls don't call fn again. They return the first call's result, whatever arguments they're given.

Write this
function once(fn) {
  // your code
}
 
// const init = once((name) => { console.log("init", name); return 42; });
// init("a"); // logs "init a", returns 42
// init("b"); // logs nothing, returns 42

Solve it in the editor, with tests

The answer

Write it yourself first. Then compare it with a reference solution and the checks it passes.

Checking your sign-in…