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
2021-01-13 03:31:00 +01:00
dashedName: cumulative-standard-deviation
2019-01-30 13:41:15 +05:30
---
2020-11-27 19:02:05 +01:00
# --description--
2020-03-30 11:23:18 -05:00
2020-11-27 19:02:05 +01:00
Write a function that takes an array of numbers as parameter and returns the [standard deviation ](<https://en.wikipedia.org/wiki/Standard Deviation> ) of the series.
2019-07-18 17:32:12 +02:00
2020-11-27 19:02:05 +01:00
# --hints--
2020-03-30 11:23:18 -05:00
2020-11-27 19:02:05 +01:00
`standardDeviation` should be a function.
2019-01-30 13:41:15 +05:30
2020-11-27 19:02:05 +01:00
```js
assert(typeof standardDeviation == 'function');
```
2020-03-30 11:23:18 -05:00
2020-11-27 19:02:05 +01:00
`standardDeviation([2, 4, 4, 4, 5, 5, 7, 9])` should return a number.
2019-01-30 13:41:15 +05:30
2020-11-27 19:02:05 +01:00
```js
assert(typeof standardDeviation([2, 4, 4, 4, 5, 5, 7, 9]) == 'number');
```
2019-01-30 13:41:15 +05:30
2020-11-27 19:02:05 +01:00
`standardDeviation([2, 4, 4, 4, 5, 5, 7, 9])` should return `2` .
2020-03-30 11:23:18 -05:00
2020-11-27 19:02:05 +01:00
```js
assert.equal(standardDeviation([2, 4, 4, 4, 5, 5, 7, 9]), 2);
```
2019-01-30 13:41:15 +05:30
2020-11-27 19:02:05 +01:00
`standardDeviation([600, 470, 170, 430, 300])` should return `147.323` .
```js
assert.equal(standardDeviation([600, 470, 170, 430, 300]), 147.323);
2019-01-30 13:41:15 +05:30
```
2020-11-27 19:02:05 +01:00
`standardDeviation([75, 83, 96, 100, 121, 125])` should return `18.239` .
2019-01-30 13:41:15 +05:30
2020-11-27 19:02:05 +01:00
```js
assert.equal(standardDeviation([75, 83, 96, 100, 121, 125]), 18.239);
```
2020-03-30 11:23:18 -05:00
2020-11-27 19:02:05 +01:00
`standardDeviation([23, 37, 45, 49, 56, 63, 63, 70, 72, 82])` should return `16.87` .
2019-07-18 17:32:12 +02:00
2020-11-27 19:02:05 +01:00
```js
assert.equal(
standardDeviation([23, 37, 45, 49, 56, 63, 63, 70, 72, 82]),
16.87
);
```
`standardDeviation([271, 354, 296, 301, 333, 326, 285, 298, 327, 316, 287, 314])` should return `22.631` .
```js
assert.equal(
standardDeviation([
271,
354,
296,
301,
333,
326,
285,
298,
327,
316,
287,
314
]),
22.631
);
```
# --seed--
## --seed-contents--
2019-01-30 13:41:15 +05:30
```js
2019-02-26 17:07:07 +09:00
function standardDeviation(arr) {
2020-09-15 09:57:40 -07:00
2019-01-30 13:41:15 +05:30
}
```
2020-11-27 19:02:05 +01:00
# --solutions--
2019-01-30 13:41:15 +05:30
```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
}
```