3.8 KiB
3.8 KiB
id, title, challengeType, videoUrl, localeTitle
id | title | challengeType | videoUrl | localeTitle |
---|---|---|---|---|
587d7b8e367417b2b2512b5c | Understand Functional Programming Terminology | 1 | 理解功能编程术语 |
Description
getTea
功能,以处理各种茶叶请求。我们可以修改getTea
来接受一个函数作为参数,以便能够改变它准备的茶的类型。这使得getTea
更加灵活,并且在客户端请求发生变化时为程序员提供更多控制。但首先,让我们介绍一些函数术语: Callbacks
函数是滑动或传递给另一个函数来决定函数调用的函数。您可能已经看到它们传递给其他方法,例如在filter
,回调函数告诉JavaScript如何过滤数组的标准。可以分配给变量,传递到另一个函数或从其他函数返回的函数就像任何其他正常值一样,称为first class
函数。在JavaScript中,所有函数都是first class
函数。将函数作为参数或将函数作为返回值返回的函数称为higher order
函数。当函数传递给另一个函数或从另一个函数返回时,那些传入或返回的函数可以称为lambda
。 Instructions
tea4GreenTeamFCC
和tea4BlackTeamFCC
变量中。请注意, getTea
函数已被修改,因此它现在将函数作为第一个参数。注意:数据(茶杯数量)作为最后一个参数提供。我们将在后面的课程中对此进行更多讨论。 Tests
tests:
- text: <code>tea4GreenTeamFCC</code>变量应该为团队提供27杯绿茶。
testString: 'assert(tea4GreenTeamFCC.length === 27, "The <code>tea4GreenTeamFCC</code> variable should hold 27 cups of green tea for the team.");'
- text: <code>tea4GreenTeamFCC</code>变量应该拿着一杯绿茶。
testString: 'assert(tea4GreenTeamFCC[0] === "greenTea", "The <code>tea4GreenTeamFCC</code> variable should hold cups of green tea.");'
- text: <code>tea4BlackTeamFCC</code>变量应该可以容纳13杯红茶。
testString: 'assert(tea4BlackTeamFCC.length === 13, "The <code>tea4BlackTeamFCC</code> variable should hold 13 cups of black tea.");'
- text: <code>tea4BlackTeamFCC</code>变量应该拿着一杯红茶。
testString: 'assert(tea4BlackTeamFCC[0] === "blackTea", "The <code>tea4BlackTeamFCC</code> variable should hold cups of black tea.");'
Challenge Seed
/**
* 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
);
Solution
// solution required