Sobes.tech
Middle

/** * Implementar a função wrapper runOnce, que aceita * uma função e devolve uma nova função. A nova * função só pode ser chamada uma vez, todas as chamadas * subsequentes retornam undefined. * * A função envolvida pode aceitar argumentos * e retornar um resultado. */ function runOnce(fn) { // o seu código } const logHello = (name) => { console.log(`olá, ${name}!`) }; const logHelloOnce = runOnce(logHello); console.clear(); logHelloOnce('Oleg'); // 'olá, Oleg!' logHelloOnce('Olga'); // undefined

sobes.tech IA

Resposta da IA

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


Функция `runOnce` создаёт замыкание с флагом `called`. При первом вызове обёрнутой функции она вызывает исходную функцию с переданными аргументами и устанавливает флаг. Все последующие вызовы возвращают `undefined` и не вызывают исходную функцию.