Middle+
Implement a runOnce wrapper function that takes a function and returns a new function. The new function can only be called once, and all subsequent calls return undefined. The wrapped function can take arguments and return a result.
sobes.tech AI
Answer from AI
javascript function runOnce(fn) { let called = false; return function(...args) { if (called) return undefined; called = true; return fn.apply(this, args); }; }
const logHello = (name) => {
console.log(hello, ${name}!);
};
const logHelloOnce = runOnce(logHello); console.clear(); logHelloOnce('Oleg'); // 'hello, Oleg!' logHelloOnce('Olga'); // undefined
The `runOnce` function returns a new function that, upon its first call, invokes the original function with the provided arguments and returns its result. On subsequent calls, it returns `undefined` and does not invoke the original function.