Understand Functional Programming Terminology -- help

Tell us what’s happening:

Hi, could someone explain why is there a +=1 in the function code block instead of++?

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.106 Safari/537.36.

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

They are the same thing written with different syntax, they have nothing to do with functional programming terminology.

You mean it could be used interchangeably?

You mean it could be used interchangeably?

Yes, in most cases.

They aren’t exactly equivalent, although the differences are pretty small and language/implementation dependent. The important thing to note is that you will generally read and use the two differently. i++ or ++i means “Increment variable i and return it (or return i pre-increment)”. i+=1 means “assign i+1 to variable i”.

++ or – always increments or decrements by a fixed amount. += allows you to define the step. So I can have i+=2 if I want to skip numbers. I can also have i*=2 if I want to double each step. If you are only incrementing by 1, it’s more elegant to use the ++ operator. You only see +=1 when a programmer is new, writing or translating from a language without increment operations (e.g. python), or they used a different step at one point and altered it later.

But yes, for all intents and purposes, i++ is going to be the same as i+=1. And that has nothing to do with functional programming.

1 Like