JavaScript: Function Composition Problem (LeetCode)

Think With Me
3 min readDec 17, 2023

--

This problem was quite interesting. Lets start with the instructions:

Given an array of functions [f1, f2, f3, …, fn], return a new function fn that is the function composition of the array of functions.

The function composition of [f(x), g(x), h(x)] is fn(x) = f(g(h(x))).

The function composition of an empty list of functions is the identity function f(x) = x.

You may assume each function in the array accepts one integer as input and returns one integer as output.

I think the unique thing about this problem is it’s asking us to work backwards when resolving x. Let’s start from the top.

Given an array of functions [f1, f2, f3, …, fn], return a new function fn that is the function composition of the array of functions.

This sentence is stating that there will be an array of functions (could be 1, could be hundreds), and the problem would like us to return the result of a new function that is the composition of all of the functions. Well, to understand this we must first understand what a composition is. Mathematically, a composition means to apply one function to the results of another. The problem is asking us to use the results of one function as the argument for another.

The function composition of [f(x), g(x), h(x)] is fn(x) = f(g(h(x))).

Lets start with the first half:

[f(x), g(x), h(x)]

Don’t let this confuse you, this is simply an array of functions with each letter (f, g, h) representing a different function.

fn(x) = f(g(h(x))).

We are given what the value of x should be, and as you can see we are working backwards. First we calculate h(x), and whatever the result of that is will then be used in the g function: g(h(x)) is the same as g(the result of h(x)). Then we move to the f function, which will essentially be the same steps, so it should be the result of g(h(x)).

The function composition of an empty list of functions is the identity function f(x) = x.

This means if an array is empty, the composition should be the value of x.

Still with me? Cool! Let’s solve this!

var compose = function(functions) {

return function(x) {
let result = x;
for(let i = functions.length - 1; i >= 0; i--){
result = functions[i](result);
}
return result;
}
};

Above, we have a function called compose, which takes an array of functions as an argument. Within the compose function is an anonymous function that takes an integer. Within the anonymous function, we have a variable called result which is set to whatever the value of x is.

for(let i = functions.length - 1; i >= 0; i--){

}

We then use a for loop. A for loop is a good choice of loop for this situation since we are needing to work backwards. In the for loop, we set the index to the length of the functions of array — 1. Since arrays are zero indexed, we need to minus one to get the true number of items in the array. If we didn’t, we would get an error because the program would look for an index that doesn’t exist.

for(let i = functions.length - 1; i >= 0; i--){

}

We also set the condition to i ≥ 0, because we know that the 0 index is the last function in the array (remember, we are working backwards), and until the condition is false, we want the program to minus 1 from the index (hence i — — at the end).

var compose = function(functions) {

return function(x) {
let result = x;
for(let i = functions.length - 1; i >= 0; i--){
result = functions[i](result);
}
return result;
}
};
result = functions[i](result);

Next, we are reassigning the value of result to the return value of the function. Notice we are also passing the value of result into the function. We are using the for loop and index to loop through every function inside of the array, hence we are using the index for our array of functions (functions[i]). So, functions[i] is the specific function inside of the array at position [i]. Result is being passed into the specific function and then being reassigned to result. This constantly updates result until all functions have ran, and at the 0 index it runs again then stops and returns result. At this point, result should be the composition of f(g(h(x))).

Whew! We flew right through this one. If you guys have any questions or need me to explain something clearer please feel free to drop a line down in the comments! I’d love to assist. Until next time!

--

--

Think With Me
0 Followers

Here to share my thought process as it pertains to coding exercises, challenges, and logic.