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` возвращает новую функцию, которая при первом вызове вызывает исходную функцию с переданными аргументами и возвращает её результат. При последующих вызовах возвращает `undefined` и не вызывает исходную функцию.