2019-01-30 13:41:15 +05:30
---
id: 5a23c84252665b21eecc7e03
title: Cumulative standard deviation
challengeType: 5
2020-05-21 17:31:25 +02:00
isHidden: false
2019-08-05 09:17:33 -07:00
forumTopicId: 302240
2019-01-30 13:41:15 +05:30
---
## Description
2020-03-30 11:23:18 -05:00
2019-01-30 13:41:15 +05:30
<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.
2020-03-30 11:23:18 -05:00
2019-01-30 13:41:15 +05:30
</section>
## Instructions
2020-03-30 11:23:18 -05:00
2019-01-30 13:41:15 +05:30
<section id='instructions'>
</section>
## Tests
2020-03-30 11:23:18 -05:00
2019-01-30 13:41:15 +05:30
<section id='tests'>
2020-03-30 11:23:18 -05:00
```yml
2019-01-30 13:41:15 +05:30
tests:
- text: <code>standardDeviation</code> should be a function.
2020-03-30 11:23:18 -05:00
testString: assert(typeof standardDeviation == 'function');
2019-01-30 13:41:15 +05:30
- text: <code>standardDeviation([2, 4, 4, 4, 5, 5, 7, 9])</code> should return a number.
2020-03-30 11:23:18 -05:00
testString: assert(typeof standardDeviation([2, 4, 4, 4, 5, 5, 7, 9]) == 'number');
2019-01-30 13:41:15 +05:30
- text: <code>standardDeviation([2, 4, 4, 4, 5, 5, 7, 9])</code> should return <code>2</code>.
2020-03-30 11:23:18 -05:00
testString: assert.equal(standardDeviation([2, 4, 4, 4, 5, 5, 7, 9]), 2);
2019-01-30 13:41:15 +05:30
- text: <code>standardDeviation([600, 470, 170, 430, 300])</code> should return <code>147.323</code>.
2020-03-30 11:23:18 -05:00
testString: assert.equal(standardDeviation([600, 470, 170, 430, 300]), 147.323);
2019-01-30 13:41:15 +05:30
- text: <code>standardDeviation([75, 83, 96, 100, 121, 125])</code> should return <code>18.239</code>.
2020-03-30 11:23:18 -05:00
testString: assert.equal(standardDeviation([75, 83, 96, 100, 121, 125]), 18.239);
2019-01-30 13:41:15 +05:30
- text: <code>standardDeviation([23, 37, 45, 49, 56, 63, 63, 70, 72, 82])</code> should return <code>16.87</code>.
2020-03-30 11:23:18 -05:00
testString: assert.equal(standardDeviation([23, 37, 45, 49, 56, 63, 63, 70, 72, 82]), 16.87);
2019-01-30 13:41:15 +05:30
- text: <code>standardDeviation([271, 354, 296, 301, 333, 326, 285, 298, 327, 316, 287, 314])</code> should return <code>22.631</code>.
2020-03-30 11:23:18 -05:00
testString: assert.equal(standardDeviation([271, 354, 296, 301, 333, 326, 285, 298, 327, 316, 287, 314]), 22.631);
2019-01-30 13:41:15 +05:30
```
</section>
## Challenge Seed
2020-03-30 11:23:18 -05:00
2019-01-30 13:41:15 +05:30
<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
2020-03-30 11:23:18 -05:00
2019-01-30 13:41:15 +05:30
<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;
2020-03-30 11:23:18 -05:00
});
2019-01-30 13:41:15 +05:30
2020-03-30 11:23:18 -05:00
var std_dev = Math.sqrt(sum_sq / n - Math.pow(sum / n, 2));
return Math.round(std_dev * 1000) / 1000;
2019-01-30 13:41:15 +05:30
}
```
2019-07-18 17:32:12 +02:00
</section>