Why use functions here? Understand Functional Programming Terminology

Why use functions here? Understand Functional Programming Terminology
0.0 0

#1

Tell us what’s happening:

Hello all, I have complete this challenge but I am a little confused by the 2 declarations up top, prepareGreenTea and prepareBlackTea. If all they do is a return a string, why make them functions at all?

Could you just write: const prepareGreenTea = ‘green tea’; ?

If you did that, couldn’t you eliminate one step in the for loop, and just push green or black tea x times to the empty array?

Thank you for your help!

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 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36.

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


#2

With functional programming you want to try to express everything in terms of functions. In general, you should avoid global variables.

This challenge is very basic and is merely to introduce you to the concept of passing functions to other functions and calling those functions. Each of the functions prepareGreenTea and prepareBlackTea only do one thing and always return the same value. This is a very important concept in functional programming as you learn.


#3

They are very unnecessary and have nothing to do with functional programming.
I’d prefer your alternative over this one which involves unnecessary function call.