找出数组中的最大值
function max(arr) {
let max = arr[0];
for (let i = 1; i < arr.length; i++) {
if (arr[i] > max) {
max = arr[i];
}
}
}
Math.max.call(null, arr);
计算数组元素的平均值
function average(arr) {
let sum = 0,
len = arr.length;
for (let i = 0; i < len; i++) {
sum += arr[i];
}
const average = sum / len;
return average;
}
复制数组
function copyArr(arr) {
let tempArr = [],
len = arr.length;
for (let i = 0; i < len; i++) {
tempArr[i] = arr[i];
}
return tempArr;
}
function copyArr(arr) {
return arr.slice();
}
颠倒数组顺序
function reverseArr(arr) {
let tempArr = [],
len = arr.length;
for (let i = 0; i < len / 2; i++) {
tempArr[i] = arr[len - i - 1];
}
return tempArr;
}