html代码:
js代码: // 获取参数 function getKnowMoMe(param,flag){ let a = typeof(param); console.log(a); let mo,me; if(a == 'object'){ //数组 getMo(param); }else{ //非数组-字符串 let p = param.split(flag) getMo(p); } } // 获取众数 function getMo(p){ let np={}; p.map(function(ele,num){ if(Object.keys(np).length != 0){ if(!!np[ele]){ np[ele] = np[ele]+1; } if(!np[ele]){ np[ele] = 1; } }else{ np[ele] = 1; } }) let max = getMax(np); let mm = getMuchMax(np,max) let na = makeArray(mm); console.log(na); getMe(paoOrder(na)); console.log(np); } // 获取中位数 function getMe(p){ p = paoOrder(p); let a = p.length%2; if(a == 0){ let n = p.length/2; let me = (p[n]+p[n+1])/2 console.log(p.toString()+'的中位数为:'+me); }else{ let n = Math.ceil(p.length/2); let me = p[n]; console.log(p.toString()+'的中位数为:'+me) } } // 数组排序 function paoOrder(p){ for(let i=0;i}
// 获取最大值有几个。
function getMuchMax(p,num){
var list = new Array();
for(var i in p){
if(p[i] == num){
let a = {
k:i,
v:p[i]
};
list.push(a);
}
}
return list;
}
// 制成数组
function makeArray§{
let a = [];
for(n in p){
for(let i=0;i<p[n].v;i++){
a[a.length] = p[n].k;
}
}
return a;
}