Day 7: Once

1 min read ❤️ 0 · 💬 1
#javascript #100daysofcode #day7

Question:

Write a function called once that takes a function as an argument and returns a new function. The new function should only call the original function once and return its result for subsequent calls.


function once(fn) {
 // Todo add your code
}

function expensiveOperation() {
  // Time-consuming calculation
  console.log("Executing expensive operation...");
  return "Result";
}

const executeOnce = once(expensiveOperation);
console.log(executeOnce()); // Result
console.log(executeOnce()); // Result

Enter fullscreen mode Exit fullscreen mode

🤫 Check the comment below to see answer.


Originally published on DEV Community . Read the discussion and share your thoughts there.

© 2026

LinkedIn 𝕏 GitHub