First let’s start with the classic procedural solution:

The first thing we notice is the accumulation through the total_surface variable, which is symptomatic of a missing reduce function:

Let’s extract each operation to its own function for a better separation of concerns:

I don’t really like the sides function because it does three times the same thing. It’s time to replace it with a recursive alternative:

Same mixed feelings with the content of the box_surface function, it looks like a reducer is missing:

Now we can delete the smallest_side function and put it in box_surface. And in the same time, remove the unnecessary parenthesis:

Since sides return an array, and box_surface make a reduction on it. We can extract the reduce instruction from the box_surface function:

Ok, but there is something we really love in the functional world: to have only pure expressions (that mean no { } characters to define lambda function’s body).

It is not possible here because box_side is used twice at line 9. So, let’s create a simple function apply to handle this:

We notice this apply function really looks like an IIFE:

At least, we can extract the boxes.reduce function (lines 7–8) and replace the box_surface function.

Now, we have only simple expressions to compute all the required steps:

Compare it with the original procedural code: