Skip to content Skip to sidebar Skip to footer

Add/edit Javascript Program To Calculate Minimum, Maximum, Total And Average Of The Array Element

myArray = new Array(1, 4, 3, 2, 7, 6, 5, 9, 8, 10, 0) document.write('
') document.write('myArray.toString()\t:\t\t') document.write(myArray.toString()+'') document.write

Solution 1:

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;

Post a Comment for "Add/edit Javascript Program To Calculate Minimum, Maximum, Total And Average Of The Array Element"