Understand Functional Programming Terminology Help

Tell us what’s happening:
Can anyone help me with this challenge?
I am using the prepareGreenTea function as the first parameter in the getTea function, and am doing the same for the second getTea function.
The error I get is, ‘prepare tea is not a function.’
I have used green and black tea functions respectively, to store their values.
Your code so far


/**
 * A long process to prepare green tea.
 * @return {string} A cup of green tea.
 **/
const prepareGreenTea = () => 'greenTea';

/**
 * A long process to prepare black tea.
 * @return {string} A cup of black tea.
 **/
const prepareBlackTea = () => 'blackTea';

/**
 * Get given number of cups of tea.
 * @param {function():string} prepareTea The type of tea preparing function.
 * @param {number} numOfCups Number of required cups of tea.
 * @return {Array<string>} Given amount of tea cups.
 **/
const getTea = (prepareTea, numOfCups) => {
  const teaCups = [];

  for(let cups = 1; cups <= numOfCups; cups += 1) {
    const teaCup = prepareTea();
    teaCups.push(teaCup);
  }

  return teaCups;
};

// Add your code below this line

const tea4GreenTeamFCC = getTea(prepareGreenTea(),27); // :(
const tea4BlackTeamFCC = getTea(prepareBlackTea(),13); // :(

// Add your code above this line

console.log(
  tea4GreenTeamFCC,
  tea4BlackTeamFCC
);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.84 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/functional-programming/understand-functional-programming-terminology

1 Like

You don’t need parenthesis as they are directly invoking the functions.

Just pass in the function names as callbacks and the functions will be called inside this function.

const teaCup = prepareTea();

1 Like

I did remove the parenthesis at the end, and it did work. However, don’t we always have to use the parenthesis to call a function?

In this case, you shouldn’t because getTea() will call prepareTea(). Passing the function as an argument and passing the output of function is not always equivalent. Think about how callbacks work.

2 Likes

Just to better understand this, when we pass functions as arguments to other functions, do we include the parenthesis? I am asking because I am very new to callbacks and related stuff. For example, if we call a regular function, we call it like this getTea();

In the case of callbacks, if we pass a callback function to another function, how would we do it?
Example, getTea(prepareTea,27); OR getTea(prepareTea(),27);

Thanks

Just to better understand this, when we pass functions as arguments to other functions, do we include the parenthesis?

It depends on the design of a function.

Suppose some function Foo takes a function argument. Foo simply calls its function argument

function Foo(fn) {
    return fn()
}

Now, consider two functions A, B.

function A() {
     return 1
}

function B() {
    return function() {
        return 2
   }
}

Now, A returns primitive value and B returns a function.

So, the passing of A and B to Foo must be handled differently.

Foo( A )
Foo( B() )

Get it?

2 Likes

I really appreciate the example, but why are they handled differently?

If we pass B as an argument to foo, the foo function will call B automatically because of this statement return fn().

Since B returns a function and foo calls a function, the return value of Foo( B ) is the inner function of B.

But if you pass Foo( B() ), then you are passing the function returned by B to Foo. So, in this case, Foo calls the inner function of B. In other words, the return value is the return value of the inner function of B.

This can go on and on, but as you layer more function into another, it gets tricky quickly.

function C() {
    return function() {
        return function() {
           return 2
       }
   }
}

Foo( C()() )
1 Like

In the getTea problem, because prepareBlackTea and prepareGreenTea returns a string and not a function, we will only pass in the name of the function and not ().
Is that right?

Thanks,
Faizan

1 Like

Yes, because string isn’t callable, you want to pass the function itself instead of its return value.

1 Like