欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

js获取整型数组最大值、最小值、平均值

程序员文章站 2022-07-05 14:53:30
恢复内容开始 恢复内容结束 ......

---恢复内容开始---

let values = [];//数组(整型数字)
//获取数组最大值
function arrmaxnum(arr){
var maxnum = null;
for (var i = 0; i < arr.length; i++) {
var item =parseint(arr[i]);
if(item){
if(!maxnum){
maxnum = item;
}
item>maxnum ? maxnum =item : null;
}
};
return maxnum;
};
//获取数组最小值(排除0)
function arrminnum(arr){
  var minnum = null;
for (var i = 0; i < arr.length; i++) {
var item =parseint(arr[i]);
if(item){
if(!minnum){
minnum = item;
}
item<minnum ? minnum =item : null;
}
};
return minnum;
};

//获取数组平均值(排除0)
function arraveragenum(arr){
var sum = 0;
var count = 0
for (var i = 0; i < arr.length; i++) {
var item =parseint(arr[i]);
if(item){
sum += item;
count++;
}
};
return ~~(sum/count*100)/100;
};

---恢复内容结束---

let values = [];//数组(整型数字)
//获取数组最大值
function arrmaxnum(arr){
var maxnum = null;
for (var i = 0; i < arr.length; i++) {
var item =parseint(arr[i]);
if(item){
if(!maxnum){
maxnum = item;
}
item>maxnum ? maxnum =item : null;
}
};
return maxnum;
};
//获取数组最小值(排除0)
function arrminnum(arr){
  var minnum = null;
for (var i = 0; i < arr.length; i++) {
var item =parseint(arr[i]);
if(item){
if(!minnum){
minnum = item;
}
item<minnum ? minnum =item : null;
}
};
return minnum;
};

//获取数组平均值(排除0)
function arraveragenum(arr){
var sum = 0;
var count = 0
for (var i = 0; i < arr.length; i++) {
var item =parseint(arr[i]);
if(item){
sum += item;
count++;
}
};
return ~~(sum/count*100)/100;
};