function Avg() {
let ary = [].slice.call(arguments);
ary.sort(function (a, b) {
return a - b;
});
//去掉最小值
ary.pop();
//去掉最大值
ary.shift();
// let total = null;
//数组求和
/*for (let i = 0; i < ary.length; i++) {
total += ary[i];
}*/
//eval:是window下的一个方法;把字符串转换成真正的表达式,进行计算
let total = eval(ary.join("+"));
return total / ary.length;
}
console.log(Avg(1, 2, 3, 4, 5));//=>3
求一组数的和
循环实参集合,把每一项相加
function sum() {
let total = null;
for (let i = 0; i < arguments.length; i++) {
total += arguments[i];
}
return total;
}
console.log(sum(1, 2, 1, 3, 4, 8, 7));//=>26
调用数组的join方法,再调用window下的eval()
function sum() {
let ary = [].slice.call(arguments);
let total = eval(ary.join("+"));
return total;
}
console.log(sum(1, 2, 1, 3, 4, 8, 7));//=>26