我尝试了几乎所有东西,但似乎无法将最终结果作为一个最多2位小数的圆数。这是一个标准偏差
提前感谢,这是代码。
function round(number) {
var final = Math.round(number * 100) / 100;
return final;
}
function stdev(arr) {
var avg = average(arr);
var squareDiff = arr.map(function(arr) {
var diff = arr - avg;
var sqrDiff = diff * diff;
return sqrDiff;
});
var avgSquareDiff = average(squareDiff);
var stdDev = Math.sqrt(avgSquareDiff);
return stdDev;
function average(data) {
var sum = data.reduce(function(sum, arr) {
return sum + arr;
}, 0);
return round(sum / data.length);
}
}
答案 0 :(得分:2)
您在average
返回时对该值进行四舍五入。
但是,在stdev
中,您可以计算该舍入值的平方根。
该操作很可能会返回一个包含大量小数的数字,并且您可以直接返回它而不会再次舍入它。
很可能你需要
function round(number) {
var final = Math.round(number * 100) / 100;
return final;
}
function average(data) {
var sum = data.reduce(function(sum, arr) {
return sum + arr;
}, 0);
return sum / data.length;
}
function stdev(arr) {
var avg = average(arr);
var squareDiff = arr.map(function(arr) {
var diff = arr - avg;
var sqrDiff = diff * diff;
return sqrDiff;
});
var avgSquareDiff = average(squareDiff);
var stdDev = Math.sqrt(avgSquareDiff);
return round(stdDev);
}