Add/Edit Javascript program to calculate minimum,

2019-09-30 18:01发布

myArray = new Array(1, 4, 3, 2, 7, 6, 5, 9, 8, 10, 0)
document.write("<pre>")
document.write("myArray.toString()\t:\t\t")
document.write(myArray.toString()+"")
document.write("myArray.join('-')\t:\t\t")
document.write(myArray.join ("-")+"")
document.write("myArray.reverse()\t:\t\t")
document.write(myArray.reverse()+"")
document.write("myArray.sort()\t\t:\t\t")
document.write(myArray.sort()+"")

1条回答
不美不萌又怎样
2楼-- · 2019-09-30 18:20

Minimum and maximum

var min = Math.min.apply(null, myArray);
var max = Math.max.apply(null, myArray);

Total

var sum = myArray.reduce(function(a, b) {
    return a + b;
});

Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce

Average

Just divide sum by the number of array elements:

var average = myArray.reduce(function(a, b) {
    return a + b;
})/myArray.length;
查看更多
登录 后发表回答