While working on some Codewars' coding challenges, I came across this problem: Calculating with Functions, very straight forward for a javascript challenge.

Requirements are the following: There must be a function for each number between 0–9, a function for every mathematical operation, each calculation consist of one operation and two numbers. The Division operation should return a integer not float or decimal. The goal of this article is to share the information I've came across while trying to solve this problem.

seven(times(five())); // must return 35
four(plus(nine())); // must return 13
eight(minus(three())); // must return 5
six(dividedBy(two())); // must return 3

During my first try, I understood that the number functions must return the appropriate number then I ran into my first problem. When running this it only returns the number and doesn't apply the operations.

function five(){ return 5}
function seven() { return 7 }

What about the operation functions? These operation functions receives the variables from the upper function and applies the inner function to its own operation. How do we do this exactly.

Introducing our first lesson Javascript functions

Calling functions.

Functions themselves are objects, so they have their own functions and methods more on this later. They can be called within each other or nested functions and closures. Closures are created during nested functions, they are an expression that allows the nested function to inherit the arguments and variables of its containing function. Since the inner function forms a closure, you can call the outer function and specify arguments for both the outer and inner function. Looking back at our problem.

seven(times(five()));
// Times and five both have access to the variables declared in the seven and times function.

What does this mean exactly? This allows the nested function to inherit the arguments and variables of its containing function. We know that seven returns the integer 7 to the times function here closures become important. The times operation function must bring the two numbers declared in the outer and inner functions together. How?

First we know that the times function accepts a number function easy. Let's name it b, you'll see why soon.

// This takes care of the second number but what about the first number? 
Because of closures we know that we have access to the first return number but how do get access? 
function times(b) {}

Well actually we don't we must change our seven function to check if it has any arguments and if it does apply those arguments to its returning integer. Introducing our next lesson on functions

The arguments object.

The arguments of a function are maintained in an array-like object. Within a function you can address the arguments passed to it as follows.

arguments[0]
// this returns the first argument of the function this keyword was in. arguments[1] returns the second argument  and so on.

This is one of those function object methods that I mentioned above. There are many others such as: Caller, Displayname, Length, Name. These are all Instance properties and there are also instance methods.


// We need to have a conditional if there's a argument/ ie the times operation function, apply the number 7 to that argument. 
// Feel free to console log what arguments[0] returns. It should return a function our times function exactly.
function seven () {
 return arguments.length ? arguments[0](7) : 7
 // This line checks if there's any arguments and if there is apply seven to the function.
 }

Okay this is our completed integer function this is the same for all of our functions. 

Now, back to our operation function. So if 7 is being passed down to our times function but we already have 5 being passed as a argument how to we get access to the 7? Do we pass in another argument yes but not exactly. So remember closures give access to the outer function variables in the inner function. We can defined another function within our times function and pass in the 7 there and have access to the five because it was defined in the outer function. Let's check it out.

function times(b) { return function(a) {
 return Math.floor(a * b) }}

The inner function does all the work and the outer function returns that function and accepts the 5 returned by the function. And that's pretty much it. Here is our functions all together.

function seven() {
return arguments.length ? arguments[0](7) : 7; }

function times(b) {
return function(a) { return Math.floor(a * b) } }

function five() {
return arguments.length ? arguments[0](5) : 5; }
seven(times(five()))

This was a great kata that taught me alot. Javascript is pretty weird and interesting. Thanks for reading and if you're need more example take a look at Daily Coding Puzzles

She gives a different look at all of the languages. Also take a look at the Javascript docs on Mozilla. Thanks for reading and have a nice day.

Blog

copyright©2021 Keenan all rights reserved