--- id: 587d7b8e367417b2b2512b5c title: Understand Functional Programming Terminology challengeType: 1 videoUrl: '' localeTitle: 理解功能编程术语 --- ## Description
联邦通信委员会团队有一种情绪波动,现在想要两种类型的茶:绿茶和红茶。一般事实:客户情绪波动很常见。有了这些信息,我们需要重新审视上次挑战中的getTea功能,以处理各种茶叶请求。我们可以修改getTea来接受一个函数作为参数,以便能够改变它准备的茶的类型。这使得getTea更加灵活,并且在客户端请求发生变化时为程序员提供更多控制。但首先,让我们介绍一些函数术语: Callbacks函数是滑动或传递给另一个函数来决定函数调用的函数。您可能已经看到它们传递给其他方法,例如在filter ,回调函数告诉JavaScript如何过滤数组的标准。可以分配给变量,传递到另一个函数或从其他函数返回的函数就像任何其他正常值一样,称为first class函数。在JavaScript中,所有函数都是first class函数。将函数作为参数或将函数作为返回值返回的函数称为higher order函数。当函数传递给另一个函数或从另一个函数返回时,那些传入或返回的函数可以称为lambda
## Instructions
准备27杯绿茶和13杯红茶,分别储存在tea4GreenTeamFCCtea4BlackTeamFCC变量中。请注意, getTea函数已被修改,因此它现在将函数作为第一个参数。注意:数据(茶杯数量)作为最后一个参数提供。我们将在后面的课程中对此进行更多讨论。
## Tests
```yml tests: - text: tea4GreenTeamFCC变量应该为团队提供27杯绿茶。 testString: assert(tea4GreenTeamFCC.length === 27); - text: tea4GreenTeamFCC变量应该拿着一杯绿茶。 testString: assert(tea4GreenTeamFCC[0] === 'greenTea'); - text: tea4BlackTeamFCC变量应该可以容纳13杯红茶。 testString: assert(tea4BlackTeamFCC.length === 13); - text: tea4BlackTeamFCC变量应该拿着一杯红茶。 testString: assert(tea4BlackTeamFCC[0] === 'blackTea'); ```
## Challenge 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} 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 ); ```
## Solution
```js // solution required ```