Javascript: Cant get a round number -
i tried cant seem final result round number 2 decimal digits. it's standard deviation
thank in advance, here code.
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); } }
you round value when return in average
.
however, in stdev
, calculate square root of rounded value.
that operation return number lots of decimals, , return directly without rounding again.
most need
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); }
Comments
Post a Comment