Calculating using functions - can someone explain?

Hi there, I have a solution to this but i’m not sure i understand it 100%

Taking (seven(times(five())) as an example:

  1. firstly five() doesn’t take in anything so return 5:
    seven(times(5))

  2. (times(5)): times takes in 5 as “number”. we pass 5 into the times function

  3. ?? now i get confused.

I wonder if anyone has a good way of explaining the step by step of this?

thanks as always

const zero = (operation) => !operation ? 0 : operation(0);
const one = (operation) => !operation ? 1 : operation(1);
const two = (operation) => !operation ? 2 : operation(2);
const three = (operation) => !operation ? 3 : operation(3);
const four = (operation) => !operation ? 4 : operation(4);
const five = (operation) => !operation ? 5 : operation(5);
const six = (operation) => !operation ? 6 : operation(6);
const seven = (operation) => !operation ? 7 : operation(7);
const eight = (operation) => !operation ? 8 : operation(8);
const nine = (operation) => !operation ? 9 : operation(9);

const plus = (number) => (other_number) => other_number + number;
const minus = (number) => (other_number) => other_number - number;
const times = (number) => (other_number) => other_number * number;
const dividedBy = (number) => (other_number) => other_number / number;

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

So like in math, things in brackets go first, right? So:

  1. Correct
  2. times(5) returns a function that will multiply 5 to a number that will be passed as argument
  3. seven(fn) has function from step 2 as argument, so it cannot just return 7, but runs fn passing 7 as argument and returns the result.

Hope this will help

Step through it. Just as iiiked described above:

  1. five() returns 5
  2. times(5) returns the function (other_number) => other_number * 5;
  3. seven( (other_number) => other_number * 5 ) executes 7 * 5 and returns 35

Hi,

Step 2: If there are two arrow functions, the argument is passed into the first function and then the second function with the logic is executed? I didn’t know this.

so another example would be:
four(plus(nine()

  1. four(plus(9)
    2)(other_number) => other_number + 9)

step 3: is this where operation(4) is called?

const four = (operation) => !operation ? 4 : operation(4);`

such that:

const four = (other_number) => other_number + 9) => 4 + 9

whereby other_number is the argument passed into the operation parameter?

is this the right thinking?

Yes, you seem to be on the right track.

The function you list in 2) is used as the argument operation in the call to four().

I am getting the value of each number and operation into one common function then processing it. Why it is not correct?

let i=0;
let a=[];
function zero() {
process(0)}
function one() {
process(1)}
function two() {
process(2)}
function three() {
process(3)}
function four() {
process(4)}
function five(a) {

//console.log(" Hii Five")
process(5)
}
function six() {
process(6)}
function seven() {

//console.log("hii seven")
process(7)
}
function eight() {
process(8)}
function nine() {
process(9)}


function plus() {
process('+')}
function minus() {
process('-')}
function times() {
//console.log("times hiii")
process('*')
}
function dividedBy() {
process('/')}

function process(args)
{
i=i+1

a[i]=args
//console.log("a is "+ a[i])
//console.log(a[1],a[2],a[3])
if(a[2]=='*' && a[3]!==undefined)
{i=0 
r=(a[1]*a[3])
a=[]
console.log(r)
}

if(a[2]=='+' && a[3]!==undefined)
{ i=0
r=(a[1]+a[3])

a=[]
console.log(r)
}

if(a[2]=='-' && a[3]!==undefined)
{ i=0
r=(a[3]-a[1])

a=[]
console.log(r)
}

if(a[2]=='/' && a[3]!==undefined)
{ i=0
r=(a[3]/a[1])

a=[]
console.log(r)
}


}