freeCodeCamp/curriculum/challenges/english/08-coding-interview-prep/rosetta-code/cumulative-standard-deviati...

2.8 KiB

id title challengeType
5a23c84252665b21eecc7e03 Cumulative standard deviation 5

Description

Write a function that takes an array of numbers as parameter and returns the standard deviation of the series.

Instructions

Tests

tests:
  - text: <code>standardDeviation</code> should be a function.
    testString: assert(typeof standardDeviation == 'function', '<code>standardDeviation</code> should be a function.');
  - text: <code>standardDeviation([2, 4, 4, 4, 5, 5, 7, 9])</code> should return a number.
    testString: assert(typeof standardDeviation([2, 4, 4, 4, 5, 5, 7, 9]) == 'number', '<code>standardDeviation([2, 4, 4, 4, 5, 5, 7, 9])</code> should return a number.');
  - text: <code>standardDeviation([2, 4, 4, 4, 5, 5, 7, 9])</code> should return <code>2</code>.
    testString: assert.equal(standardDeviation([2, 4, 4, 4, 5, 5, 7, 9]), 2, '<code>standardDeviation([2, 4, 4, 4, 5, 5, 7, 9])</code> should return <code>2</code>.');
  - text: <code>standardDeviation([600, 470, 170, 430, 300])</code> should return <code>147.323</code>.
    testString: assert.equal(standardDeviation([600, 470, 170, 430, 300]), 147.323, '<code>standardDeviation([600, 470, 170, 430, 300])</code> should return <code>147.323</code>.');
  - text: <code>standardDeviation([75, 83, 96, 100, 121, 125])</code> should return <code>18.239</code>.
    testString: assert.equal(standardDeviation([75, 83, 96, 100, 121, 125]), 18.239, '<code>standardDeviation([75, 83, 96, 100, 121, 125])</code> should return <code>18.239</code>.');
  - text: <code>standardDeviation([23, 37, 45, 49, 56, 63, 63, 70, 72, 82])</code> should return <code>16.87</code>.
    testString: assert.equal(standardDeviation([23, 37, 45, 49, 56, 63, 63, 70, 72, 82]), 16.87, '<code>standardDeviation([23, 37, 45, 49, 56, 63, 63, 70, 72, 82])</code> should return <code>16.87</code>.');
  - text: <code>standardDeviation([271, 354, 296, 301, 333, 326, 285, 298, 327, 316, 287, 314])</code> should return <code>22.631</code>.
    testString: assert.equal(standardDeviation([271, 354, 296, 301, 333, 326, 285, 298, 327, 316, 287, 314]), 22.631, '<code>standardDeviation([271, 354, 296, 301, 333, 326, 285, 298, 327, 316, 287, 314])</code> should return <code>22.631</code>.');

Challenge Seed

function standardDeviation (arr) {
  // Good luck!
}

Solution

function standardDeviation (arr) {
  var sum = 0,
    sum_sq = 0,
    n = arr.length;
  arr.forEach(function(e) {
    sum += e;
    sum_sq += e * e;
  })

  var std_dev=Math.sqrt((sum_sq / n) - Math.pow(sum / n, 2))
  return Math.round(std_dev*1000)/1000;
}