StdDev: Rosetta Code: Overshooting Stddev: Help Please

Tell us what’s happening:
The values are being properly passed due to internal returns, but the std-dev, variance, means seem to be overshooting the projected values (rounded to 3 decimal places…), sometimes minutely, others somewhat radically. Can anybody help me understand why?

Your code so far

function standardDeviation(a) {
var mean = Math.floor(
(a.reduce( (t, el) => {
return t; }) /a.length)  

console.log(`Mean is passing as ${mean}. \n`);

var variance = Math.floor((, t) => { 
 return t; }) .reduce(
(el, t) => { t+= el; 
return Math.floor(t*1000)/1000; }) /a.length)

console.log(`Variance is passing as ${variance}.\n`);

var stddev = Math.floor(Math.sqrt(variance)

console.log(`Standard deviation is
 passing as ${stddev}.\n `);

return stddev;

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36.

Challenge: Cumulative standard deviation

Link to the challenge:

Three things:

  1. The variance is not computing correctly. You can do it like the mean with one reduce summing the squares of the differences and divide the result by the number of items.
  2. Don’t round (Math.floor(... * 1000) / 1000) until the end as occasionally this can cause cumulative rounding errors, especially in longer calculations with smaller precision. It’s not a problem here though.
  3. Math.floor() truncates. The fourth test will not pass without rounding.
1 Like