2018-09-30 23:01:58 +01:00
---
id: 587d7b8d367417b2b2512b5b
title: Learn About Functional Programming
challengeType: 1
2019-08-05 09:17:33 -07:00
forumTopicId: 301233
2018-09-30 23:01:58 +01:00
---
2020-11-27 19:02:05 +01:00
# --description--
2018-09-30 23:01:58 +01:00
Functional programming is a style of programming where solutions are simple, isolated functions, without any side effects outside of the function scope.
2020-11-27 19:02:05 +01:00
`INPUT -> PROCESS -> OUTPUT`
2018-09-30 23:01:58 +01:00
Functional programming is about:
2020-11-27 19:02:05 +01:00
2018-09-30 23:01:58 +01:00
1) Isolated functions - there is no dependence on the state of the program, which includes global variables that are subject to change
2020-11-27 19:02:05 +01:00
2018-09-30 23:01:58 +01:00
2) Pure functions - the same input always gives the same output
2020-11-27 19:02:05 +01:00
2018-09-30 23:01:58 +01:00
3) Functions with limited side effects - any changes, or mutations, to the state of the program outside the function are carefully controlled
2020-11-27 19:02:05 +01:00
# --instructions--
2018-09-30 23:01:58 +01:00
The members of freeCodeCamp happen to love tea.
2020-11-27 19:02:05 +01:00
In the code editor, the `prepareTea` and `getTea` functions are already defined for you. Call the `getTea` function to get 40 cups of tea for the team, and store them in the `tea4TeamFCC` variable.
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
# --hints--
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
The `tea4TeamFCC` variable should hold 40 cups of tea for the team.
```js
assert(tea4TeamFCC.length === 40);
2018-09-30 23:01:58 +01:00
```
2020-11-27 19:02:05 +01:00
The `tea4TeamFCC` variable should hold cups of green tea.
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
```js
assert(tea4TeamFCC[0] === 'greenTea');
```
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
# --seed--
## --seed-contents--
2018-09-30 23:01:58 +01:00
```js
2020-03-04 13:08:54 -06:00
// Function that returns a string representing a cup of green tea
2018-09-30 23:01:58 +01:00
const prepareTea = () => 'greenTea';
2020-03-04 13:08:54 -06:00
/*
Given a function (representing the tea type) and number of cups needed, the
following function returns an array of strings (each representing a cup of
a specific type of tea).
*/
2018-09-30 23:01:58 +01:00
const getTea = (numOfCups) => {
const teaCups = [];
2018-10-08 01:01:53 +01:00
2018-09-30 23:01:58 +01:00
for(let cups = 1; cups < = numOfCups; cups += 1) {
const teaCup = prepareTea();
teaCups.push(teaCup);
}
return teaCups;
};
2020-03-04 13:08:54 -06:00
// Only change code below this line
const tea4TeamFCC = null;
// Only change code above this line
2018-09-30 23:01:58 +01:00
```
2020-11-27 19:02:05 +01:00
# --solutions--
2018-09-30 23:01:58 +01:00
```js
2019-02-26 08:44:33 +00:00
const prepareTea = () => 'greenTea';
const getTea = (numOfCups) => {
const teaCups = [];
for(let cups = 1; cups < = numOfCups; cups += 1) {
const teaCup = prepareTea();
teaCups.push(teaCup);
}
return teaCups;
};
const tea4TeamFCC = getTea(40);
2018-09-30 23:01:58 +01:00
```