Tutorialspoint
Problem
Solution
Submissions

Function Composition

Certification: Intermediate Level Accuracy: 0% Submissions: 0 Points: 10

Write a JavaScript program to implement function composition. Function composition is a mathematical concept where you combine two or more functions to create a new function. The composition of functions f and g, denoted as (f ∘ g)(x), means f(g(x)). Create a compose function that takes multiple functions as arguments and returns a new function that applies them from right to left.

Example 1
  • Input: Functions: add5 = x => x + 5, multiply2 = x => x * 2, subtract3 = x => x - 3, Composed function: compose(add5, multiply2, subtract3), Input value: 10
  • Output: 19
  • Explanation:
    • Start with input value 10.
    • Apply subtract3 first: 10 - 3 = 7.
    • Apply multiply2 next: 7 * 2 = 14.
    • Apply add5 last: 14 + 5 = 19.
    • Composition works right to left: add5(multiply2(subtract3(10))).
    • Final result is 19.
Example 2
  • Input: Functions: square = x => x * x, double = x => x * 2, addOne = x => x + 1, Composed function: compose(square, double, addOne), Input value: 3
  • Output: 64
  • Explanation:
    • Start with input value 3.
    • Apply addOne first: 3 + 1 = 4.
    • Apply double next: 4 * 2 = 8.
    • Apply square last: 8 * 8 = 64.
    • Composition works right to left: square(double(addOne(3))).
    • Final result is 64.
Constraints
  • The compose function should accept 1 to 10 functions as arguments
  • All functions should be unary functions (accept single argument)
  • Functions can return numbers, strings, or other primitive types
  • The composition should work from right to left
  • Time Complexity: O(n) where n is the number of functions
  • Space Complexity: O(1) for the composition execution
NumberFunctions / MethodsShopifyPhillips
Editorial

Login to view the detailed solution and explanation for this problem.

My Submissions
All Solutions
Lang Status Date Code
You do not have any submissions for this problem.
User Lang Status Date Code
No submissions found.

Please Login to continue
Solve Problems

 
 
 
Output Window

Don't have an account? Register

Solution Hints

  • Use rest parameters to accept variable number of functions
  • Apply functions from right to left using reduceRight method
  • Return a new function that accepts the initial input value
  • Chain function calls by passing result of one function to the next
  • Handle edge cases like empty function list or single function
  • Use arrow functions for cleaner syntax and proper this binding

Steps to solve by this approach:

 Step 1: Accept variable number of functions using rest parameters
 Step 2: Handle edge case when no functions are provided by returning identity function
 Step 3: Handle edge case when single function is provided by returning that function
 Step 4: Return a new function that accepts the initial input value
 Step 5: Use reduceRight to apply functions from right to left
 Step 6: Pass the result of each function as input to the next function in the chain
 Step 7: Return the final result after all functions have been applied

Submitted Code :