#please someone help me understand this code on mark asterisk , its from codewars,

8 messages · Page 1 of 1 (latest)

rich ibex
#

function expression(number, operation){
if(!operation)
return number;
return operation(number);
}

function five(operation) { return expression(5, operation); }
function seven(operation) { return expression(7, operation); }

function times(x) {
return function(y) {
console.log("y",y)
return y * x;//****how this function (y) called it's parent function seven???
}
}

seven(times(five()));

languid agate
#

can you give me the link to this question

trail chasm
#

It doesn't call Seven. Seven requires a parameter of operation. That parameter is the result of the function times, which is passed the result of the function five(). So you have to work your way back. The result of five() with no parameter being passed is going to be 5. So now you have the function time being called with the parameter x=5. Notice that this function is doing currying. It is passing back a function that takes a parameter (y) and returns y * x, where we know that x = 5, The function Seven now can resolve its parameter of "operation" which in this case is a function that invokes the expression function. It has the parameters of .. the curryied function returned from times, and the parameter of 7. So it simply is running that function (parameter "operation") with 7 passed to it. The final return value of expression is going to be 7*5.

#

Seems obvious that you could extend this by writing function plus(x), minus(x) and divide(x) using the same pattern as used in function times, only with the correct operation, and you have extended the system to allow for those operations

rich ibex
rich ibex
languid agate
#

When you call seven(times(five())), here's what happens:

  1. five() is called first. Since it doesn't receive any arguments, it returns the number 5.

  2. times(five()) is called next, with five() returning 5 as the argument. times returns a function that takes a single argument y and multiplies it by 5.

  3. seven(times(five())) is called next, with times(five()) returning a function as the argument. This function is then called with 7 as the argument, so y is set to 7. The function returns y * x, which is 7 * 5(7 multiplied by 5), or 35.
    So the final result of seven(times(five())) is 35.

Does this help you to clarify the question.

ornate burrow
#

The way it's written is in reverse to the input in each function.
7x5 is called as 5x7.
You also need to calculate the results in each number as it's not wrapped in any sum function.
So there is multiple problems there.

I like doing these, so ill submit an answer later today.