.github
api-server
client
config
curriculum
challenges
_meta
arabic
chinese
english
01-responsive-web-design
02-javascript-algorithms-and-data-structures
basic-algorithm-scripting
basic-data-structures
basic-javascript
debugging
es6
functional-programming
add-elements-to-the-end-of-an-array-using-concat-instead-of-push.english.md
apply-functional-programming-to-convert-strings-to-url-slugs.english.md
avoid-mutations-and-side-effects-using-functional-programming.english.md
combine-an-array-into-a-string-using-the-join-method.english.md
combine-two-arrays-using-the-concat-method.english.md
implement-map-on-a-prototype.english.md
implement-the-filter-method-on-a-prototype.english.md
introduction-to-currying-and-partial-application.english.md
learn-about-functional-programming.english.md
pass-arguments-to-avoid-external-dependence-in-a-function.english.md
refactor-global-variables-out-of-functions.english.md
remove-elements-from-an-array-using-slice-instead-of-splice.english.md
return-a-sorted-array-without-changing-the-original-array.english.md
return-part-of-an-array-using-the-slice-method.english.md
sort-an-array-alphabetically-using-the-sort-method.english.md
split-a-string-into-an-array-using-the-split-method.english.md
understand-functional-programming-terminology.english.md
understand-the-hazards-of-using-imperative-code.english.md
use-higher-order-functions-map-filter-or-reduce-to-solve-a-complex-problem.english.md
use-the-every-method-to-check-that-every-element-in-an-array-meets-a-criteria.english.md
use-the-filter-method-to-extract-data-from-an-array.english.md
use-the-map-method-to-extract-data-from-an-array.english.md
use-the-reduce-method-to-analyze-data.english.md
use-the-some-method-to-check-that-any-elements-in-an-array-meet-a-criteria.english.md
intermediate-algorithm-scripting
javascript-algorithms-and-data-structures-projects
object-oriented-programming
regular-expressions
03-front-end-libraries
04-data-visualization
05-apis-and-microservices
06-information-security-and-quality-assurance
08-coding-interview-prep
09-certificates
portuguese
russian
spanish
schema
test
.babelrc
.editorconfig
.npmignore
.travis.yml
CHANGELOG.md
LICENSE.md
commitizen.config.js
commitlint.config.js
create-challenge-bundle.js
getChallenges.js
gulpfile.js
index.js
lib.js
md-translation.js
package-entry.js
package-lock.json
package.json
utils.js
docs
search-indexing
tools
utils
.editorconfig
.eslintignore
.eslintrc.json
.gitattributes
.gitignore
.gitpod.yml
.node-inspectorrc
.prettierignore
.prettierrc
.snyk
.travis.yml
.vcmrc
CODE_OF_CONDUCT.md
CONTRIBUTING.md
Dockerfile.tests
LICENSE.md
README.md
SECURITY.md
change_volumes_owner.sh
docker-compose-shared.yml
docker-compose.tests.yml
docker-compose.yml
lerna.json
libcimp_index_js.patch
package-lock.json
package.json
patch_npm_and_install.sh
sample.env
* Use dfn tags
* remove misused <dfn> tags
* Revert "remove misused <dfn> tags"
This reverts commit b24968a968
.
* Update curriculum/challenges/english/01-responsive-web-design/basic-html-and-html5/fill-in-the-blank-with-placeholder-text.english.md
Co-Authored-By: Randell Dawson <5313213+RandellDawson@users.noreply.github.com>
* Make "array" lowercase
Co-Authored-By: Randell Dawson <5313213+RandellDawson@users.noreply.github.com>
* Fix dfn usage
* Address last dfn tags
120 lines
4.1 KiB
Markdown
120 lines
4.1 KiB
Markdown
---
|
|
id: 587d7b8e367417b2b2512b5c
|
|
title: Understand Functional Programming Terminology
|
|
challengeType: 1
|
|
forumTopicId: 301240
|
|
---
|
|
|
|
## Description
|
|
<section id='description'>
|
|
The FCC Team had a mood swing and now wants two types of tea: green tea and black tea. General Fact: Client mood swings are pretty common.
|
|
With that information, we'll need to revisit the <code>getTea</code> function from last challenge to handle various tea requests. We can modify <code>getTea</code> to accept a function as a parameter to be able to change the type of tea it prepares. This makes <code>getTea</code> more flexible, and gives the programmer more control when client requests change.
|
|
But first, let's cover some functional terminology:
|
|
<dfn>Callbacks</dfn> are the functions that are slipped or passed into another function to decide the invocation of that function. You may have seen them passed to other methods, for example in <code>filter</code>, the callback function tells JavaScript the criteria for how to filter an array.
|
|
Functions that can be assigned to a variable, passed into another function, or returned from another function just like any other normal value, are called <dfn>first class</dfn> functions. In JavaScript, all functions are first class functions.
|
|
The functions that take a function as an argument, or return a function as a return value are called <dfn>higher order</dfn> functions.
|
|
When the functions are passed in to another function or returned from another function, then those functions which gets passed in or returned can be called a <dfn>lambda</dfn>.
|
|
</section>
|
|
|
|
## Instructions
|
|
<section id='instructions'>
|
|
Prepare 27 cups of green tea and 13 cups of black tea and store them in <code>tea4GreenTeamFCC</code> and <code>tea4BlackTeamFCC</code> variables, respectively. Note that the <code>getTea</code> function has been modified so it now takes a function as the first argument.
|
|
Note: The data (the number of cups of tea) is supplied as the last argument. We'll discuss this more in later lessons.
|
|
</section>
|
|
|
|
## Tests
|
|
<section id='tests'>
|
|
|
|
```yml
|
|
tests:
|
|
- text: The <code>tea4GreenTeamFCC</code> variable should hold 27 cups of green tea for the team.
|
|
testString: assert(tea4GreenTeamFCC.length === 27);
|
|
- text: The <code>tea4GreenTeamFCC</code> variable should hold cups of green tea.
|
|
testString: assert(tea4GreenTeamFCC[0] === 'greenTea');
|
|
- text: The <code>tea4BlackTeamFCC</code> variable should hold 13 cups of black tea.
|
|
testString: assert(tea4BlackTeamFCC.length === 13);
|
|
- text: The <code>tea4BlackTeamFCC</code> variable should hold cups of black tea.
|
|
testString: assert(tea4BlackTeamFCC[0] === 'blackTea');
|
|
|
|
```
|
|
|
|
</section>
|
|
|
|
## Challenge Seed
|
|
<section id='challengeSeed'>
|
|
|
|
<div id='js-seed'>
|
|
|
|
```js
|
|
/**
|
|
* 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 = null; // :(
|
|
const tea4BlackTeamFCC = null; // :(
|
|
|
|
// Add your code above this line
|
|
|
|
console.log(
|
|
tea4GreenTeamFCC,
|
|
tea4BlackTeamFCC
|
|
);
|
|
```
|
|
|
|
</div>
|
|
|
|
|
|
|
|
</section>
|
|
|
|
## Solution
|
|
<section id='solution'>
|
|
|
|
```js
|
|
// solution required
|
|
const prepareGreenTea = () => 'greenTea';
|
|
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;
|
|
};
|
|
|
|
const tea4BlackTeamFCC = getTea(prepareBlackTea, 13);
|
|
const tea4GreenTeamFCC = getTea(prepareGreenTea, 27);
|
|
```
|
|
|
|
</section>
|