# 章序号/节序号/节/笔记序号
codecademy,function
# 概念阐释
- 我们可以在返回的函数中加上另一个函数,这叫做**辅助函数**
- 如果我们用函数换算摄氏度到华氏度,可以写两个函数
- 我们可以使用函数来分割小块逻辑或任务,然后在需要时使用它们。 编写辅助函数有助于处理大而困难的任务,并将它们分解成更小且更易于管理的任务。
# 举例子
本章的两道练习题就是用了helper funciton,把一个任务拆解为几个函数来写。
## 用辅助函数换算摄氏度到华氏度
- 之前学过用变量换算,但那样的问题是无法重复调用同一个任务。
- 用一个函数应该也可以完成,但问题是把所有任务都放在一个篓里,不容易debug和阅读。
```js
function multiplyByNineFifths(celsius) {
return celsius * (9/5);
};
function getFahrenheit(celsius) {
return multiplyByNineFifths(celsius) + 32;
}; //调用第一组函数,实参为celsius
getFahrenheit(15); // Returns 59
```
- 摄氏度换算为华氏度的公式为:fahrenheit = celsius * (9/5) + 32
- 将两步运算拆解:
- 第一步,一个数值*(9/5)
- 第二步,第一步的步骤+32
- 得出一组数据
# 类比、比较与对比
# 问题
- 设置一个计算监测器数量的函数,行乘以列
- 设置一个计算检测器总成本的函数,数量乘以单价200
- 可以计算不同数量的总成本
*可以用回调函数来写,但函数`monitorCount`不能有参数*
**1.**
In the previous exercise, we created a function to find the number of monitors to order for an office. Now let’s write another function that uses the `monitorCount` function to figure out the price.
Below `monitorCount` Create a function declaration named `costOfMonitors` that has two parameters, the first parameter is `rows` and the second parameter is `columns`. Leave the function body empty for now.
**2.**
Time to add some code to the function body of `costOfMonitors` to calculate the total cost.
Add a `return` statement that returns the value of calling `monitorCount(rows, columns)` multiplied by `200`.
**3.**
We should save the cost to a variable.
Declare a variable named `totalCost` using the `const` keyword. Assign to `totalCost` the value of calling `costOfMonitors()` with the arguments `5` and `4` respectively.
**4.**
To check that the function worked properly, log `totalCost` to the console.
# 问题答案
```js
function monitorCount (rows, columns) {
return rows* columns;
}//行参里写下需要的参数,return后面写下操作步骤
function monitorCost(rows, columns){
return monitorCount (rows, columns)*200
}//行参里写下需要的参数,return后面写下操作步骤
let totalCost = monitorCost(5,4);//写下想要得到的实际参数,并用变量缩短调用函数的长度
console.log(totalCost);
//回调函数写法
let monitorCount = (rows,columns) => {
return rows*columns;
};
let costOfMonitors = (func) => {
return func * 200;
};
console.log(costOfMonitors(monitorCount(5,4)));
```
# 备注(经验集/错误集)
## 经验集
## 错误集
- return后面没有冒号
## 参考资料