js实现计算方差的代码
java计算方差的函数,js实现计算方差的代码教程如下:
随机输入一组数据
实现结果:计算出总体标准偏差的方差值
js实现代码如下:
function isNum(arg)
{
var args = arg;
var fad = true;
if (args == "" || args == null || args.length == 0)
{
return false;
}
args = args.toString();
for (var i = 0; i<args.length; i++)
{
if (args.substring(i,i+1) < "0" || args.substring(i, i+1) > "9")
{
if(args.substring(i, i+1) == ".")
{
if(fad == true)
fad = false;
else
return false;
}
else
{
return false;
}
}
}
return true;
}
function madd()
{
var vva = document.first.a11.value;
vva = vva.replace(' ','');
var resul;
var bb = true;
if(vva != "")
{
resul = vva.split(",");
}
for(var v=0; v<resul.length; v++)
{
var d = resul[v];
if(isNaN(d) || d == "")
{
alert("杩欎釜鏁板瓧 \""+d+"\" 涓嶆槸鏈夋晥鐨勬暟瀛�");
var bb = false;
break;
}
}
if(bb == true)
{
var tot = resul.length;
var mean=0;
document.first.r1.value = tot;
//Mean calculation
for(var c=0; c<tot; c++)
{
mean = mean+parseFloat(resul[c]);
}
mean = mean/tot;
var mean1 = Math.round(mean*100000)/100000;
document.first.r2.value = mean1;
//Variance calculation
var variance=0;
var b;
var varian;
for(var a=0; a<tot; a++)
{
variance = variance+Math.pow((parseFloat(resul[a])-mean),2);
b = tot-1;
varian = variance/b;
var varian1 = Math.round(varian*100000)/100000;
}
document.first.r3.value = varian1;
//Standard Deviation Calculation
var sd=0;
{
sd = Math.sqrt(varian);
var sd1 = Math.round(sd*100000)/100000;
}
document.first.r4.value = sd1;
//Population Standard deviation
var pop = 0;
{
pop = variance/tot;
pop1 = Math.sqrt(pop);
var pop2 = Math.round(pop1*100000)/100000;
}
document.first.r5.value = pop2;
//Variance Population Standard deviation
var varpop = 0;
{
varpop = variance/tot;
var varpop1 = Math.round(varpop*100000)/100000;
}
document.first.r6.value = varpop1;
}
}
function color(test)
{
for(var j=2; j<10; j++)
{
var myI=document.getElementsByTagName("input").item(j);
//myI.setAttribute("style",ch);
myI.style.backgroundColor=test;
}
}
function color1(test)
{
var myI=document.getElementsByTagName("table").item(0);
//myI.setAttribute("style",ch);
myI.style.backgroundColor=test;
}
显示效果:方差计算器
参考文章:http://www.itmacom.com/gl/33486.html