Practice
JavaScriptData StructuresReactConcepts
Sign in
← Back to problems

Once Guard

A Closer Look at Functionsmedium

Course · Section 10: A Closer Look at Functions · Lecture 144: Closures

Implement `onceResults(values)` that wraps a function so it returns its argument on the first call and `null` afterwards, applying it to each value and returning the results.

Sample tests

Input: onceResults([1,2,3])
Output: [1,null,null]
Input: onceResults([5])
Output: [5]

+ 1 hidden test run on Submit.

Hints

Common pitfalls
  • Forgetting to set the flag, so it never stops returning the value.

Learning resources

  • MDN: Closures
Approach & explanation (try first)

A captured boolean lets the wrapper return its argument on the first call and null on every later one.

Loading...
⌘/Ctrl + Enter

Run your code to see results.