Understand Functional Programming Terminology challenge - explanation

Hello, can anyone explain me something in this challenge. I understand most of it but this part bugs me


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

How does the function prepareTea() work? It isnt declared anywhere else, and how exactly does it return green or black tea? Can anyone explain a bit more about this function call?
thanks

this is the whole code, and it runs and passes the test…

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';


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); // :(
  



console.log(
  tea4GreenTeamFCC,
  tea4BlackTeamFCC
);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36.

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

const teaCup = prepareTea();

Will call the function passed by the parameter, which you’re passing here:

const tea4GreenTeamFCC = getTea(prepareGreenTea, 27); // prepareTea() is prepareGreenTea()
const tea4BlackTeamFCC = getTea(prepareBlackTea, 13); // prepareTea() is prepareBlackTea()
1 Like

oh, i think i understand. i come from java and i miss verbose declarations… i wasnt aware that first parameter prepareTea was a function param, i thought it was a string…

so params are essentially const getTea = (function prepareTea, int numOfCups)?

tnx

Yup, exactly. Once you’ve gone through the course, it might be an idea to look at typescript — it’s just a layer on top of JS that adds types, but it would likely make things a easier for you: the type system is based very much on C#, but it should be very familiar to a someone with a Java background.

1 Like

yes, i’ve heard of typescript, and while i havent had too much time to look into it now, as im still getting the basics of js, i think ill definitely go the route of declared types. it makes life so much easier, for me atleast…

1 Like