Instructor: On this page, we have a few functions that perform some sort of transformation to a number. Below this, we see these functions being used inside this composed function. Now, this is just a curry function, which takes an array of functions as well as an initial value.
In our case, this initial value is 4. If we run this script and everything goes correctly, we should see 21 print out. Obviously, 266 isn't our desired value, which means that there's an error in one of these functions that's being executed. Now, when working with a function chain like this, pinpointing errors can often be difficult. Luckily for us, we can create a function that will print out the value at a given point in time when this function runs.
The trace function is also a curried function. It's going to take a label and return a value. We can then use template strings to interpolate these values and print them to our console via console.log. The last thing we need to do is return this value parameter. If we forget to do this, then any preceding function will not receive the value needed to perform that function's transformation.
Now that trace is ready to go, we can add it to our code like so. As you can see, I'm passing in a label as a string value which will specify when this function is executing. Since we're working with a curried function, the value portion will be passed through to trace automatically.
Now, if I run this code again, we'll see that the results of each function are being printed out to us. At first glance, I see that the doubled function isn't returning 32, but rather 256. Thanks to trace, we can easily head right to the doubled function and find out that it's using the exponent operator when it actually should just be using the multiplication operator.
Now that that's fixed, let's run this code again. Now taking a look at our console, we'll see that the afterDoubled function is indeed printing out 32.