2019-01-30 13:41:15 +05:30
---
id: 5a23c84252665b21eecc7e03
title: Cumulative standard deviation
challengeType: 5
2019-08-05 09:17:33 -07:00
forumTopicId: 302240
2019-01-30 13:41:15 +05:30
---
## Description
<section id='description'>
2019-07-18 17:32:12 +02:00
2019-01-30 13:41:15 +05:30
Write a function that takes an array of numbers as parameter and returns the <a href="https://en.wikipedia.org/wiki/Standard Deviation">standard deviation</a> of the series.
</section>
## Instructions
<section id='instructions'>
</section>
## Tests
<section id='tests'>
``` yml
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>.');
```
</section>
## Challenge Seed
<section id='challengeSeed'>
2019-07-18 17:32:12 +02:00
2019-01-30 13:41:15 +05:30
<div id='js-seed'>
```js
2019-02-26 17:07:07 +09:00
function standardDeviation(arr) {
2019-01-30 13:41:15 +05:30
// Good luck!
}
```
</div>
</section>
## Solution
<section id='solution'>
```js
2019-02-26 17:07:07 +09:00
function standardDeviation(arr) {
2019-01-30 13:41:15 +05:30
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;
}
```
2019-07-18 17:32:12 +02:00
</section>