Once Function

Once Function
보통 함수 +20pts

Problem

Write a function that ensures another function is only called once.

Examples

Input: const fn = once(() => 1); fn(); fn();
Output: 1, 1 (첫 호출 결과만 반환)

Explanation

이 문제는 **클로저(closure)**를 사용하여 함수가 한 번만 실행되도록 래핑하는 방법을 학습합니다. ## 핵심 개념: 실행 횟수 제한 클로저를 사용하여 함수 호출 상태를 추적하고 제어합니다. ### 접근 방법 ```javascript function once(fn) { let called = false; let result; return function(...args) { if (!called) { called = true; result = fn.apply(this, args); ...

View detailed explanation →

Key Concepts

클로저 고차 함수 상태 추적 함수 래핑
Time: O(1) Space: O(1)
solution.js
Ctrl + Enter
Run tests to see results here.