<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<script type="text/javascript">
/*需求分析
打车起步价13(3公里内),之后每多一公里增加 5块钱.
用户输入公里数就可以计算打车价格
如果有拥堵情况,总价格多收取10块钱拥堵费
*/
//普通函数
// let total=13;
// function getPrice(num,flag=false){
// if(num>3){
// total +=(num-3)*5
// }
//
// flag ? total+=10 : total;
// return total;
// }
// console.log(getPrice(20,true))
//闭包
// function getPrice() {
// let total = 13;
//
// return function(num, flag = false) {
// if(num > 3) {
// total += (num - 3) * 5
// }
// flag ? total += 10 : total;
// return total;
// };
//
// }
// let res = getPrice(); //function(num, flag = false){}
// console.log(res(20, true))
//改写
let fn = (function() {
let total =+prompt("请输入公里数");//
return function(num, flag = false) {
//判断
if (num > 3) {
total += (num - 3) * 5
}
flag ? total += 10 : total;
return total;
};
}
)()
console.log(fn(20, true))
</script>
</body>
</html>