Practice
JavaScriptData StructuresReactConcepts
Sign in
← Back to problems

Apply Named Operation

A Closer Look at Functionsmedium

Course · Section 10: A Closer Look at Functions · Lecture 137: First-Class and Higher-Order Functions

Implement `applyOp(nums, op)` that maps `nums` using the operation named by `op`: `double`, `square`, or `negate`.

Sample tests

Input: applyOp([1,2,3], "double")
Output: [2,4,6]
Input: applyOp([1,2], "square")
Output: [1,4]

+ 1 hidden test run on Submit.

Hints

Common pitfalls
  • A long if/else chain instead of a function map.
  • No handling if an unknown op name is passed.

Learning resources

  • MDN: Array iteration methods
Visualize this concept: Higher-Order Functions →
Approach & explanation (try first)

A map from name to function lets you select the transform and apply it with map, the essence of higher-order design.

Loading...
⌘/Ctrl + Enter

Run your code to see results.