2021-06-15 07:49:18 +00:00
---
id: 5a23c84252665b21eecc7e03
2021-07-29 16:41:44 +00:00
title: Deviazione cumulativa standard
2021-06-15 07:49:18 +00:00
challengeType: 5
forumTopicId: 302240
dashedName: cumulative-standard-deviation
---
# --description--
2021-07-29 16:41:44 +00:00
Scrivi una funzione che prende un array di numeri come parametro e restituisce la [deviazione standard ](https://en.wikipedia.org/wiki/Standard Deviation ) della serie.
2021-06-15 07:49:18 +00:00
# --hints--
2021-07-29 16:41:44 +00:00
`standardDeviation` dovrebbe essere una funzione.
2021-06-15 07:49:18 +00:00
```js
assert(typeof standardDeviation == 'function');
```
2021-07-29 16:41:44 +00:00
`standardDeviation([2, 4, 4, 4, 5, 5, 7, 9])` dovrebbe restituire un numero.
2021-06-15 07:49:18 +00:00
```js
assert(typeof standardDeviation([2, 4, 4, 4, 5, 5, 7, 9]) == 'number');
```
2021-07-29 16:41:44 +00:00
`standardDeviation([2, 4, 4, 4, 5, 5, 7, 9])` dovrebbe restituire `2` .
2021-06-15 07:49:18 +00:00
```js
assert.equal(standardDeviation([2, 4, 4, 4, 5, 5, 7, 9]), 2);
```
2021-07-29 16:41:44 +00:00
`standardDeviation([600, 470, 170, 430, 300])` dovrebbe restituire `147.323` .
2021-06-15 07:49:18 +00:00
```js
assert.equal(standardDeviation([600, 470, 170, 430, 300]), 147.323);
```
2021-07-29 16:41:44 +00:00
`standardDeviation([75, 83, 96, 100, 121, 125])` dovrebbe restituire `18.239` .
2021-06-15 07:49:18 +00:00
```js
assert.equal(standardDeviation([75, 83, 96, 100, 121, 125]), 18.239);
```
2021-07-29 16:41:44 +00:00
`standardDeviation([23, 37, 45, 49, 56, 63, 63, 70, 72, 82])` dovrebbe restituire `16.87` .
2021-06-15 07:49:18 +00:00
```js
assert.equal(
standardDeviation([23, 37, 45, 49, 56, 63, 63, 70, 72, 82]),
16.87
);
```
2021-07-29 16:41:44 +00:00
`standardDeviation([271, 354, 296, 301, 333, 326, 285, 298, 327, 316, 287, 314])` dovrebbe restituire `22.631` .
2021-06-15 07:49:18 +00:00
```js
assert.equal(
standardDeviation([
271,
354,
296,
301,
333,
326,
285,
298,
327,
316,
287,
314
]),
22.631
);
```
# --seed--
## --seed-contents--
```js
function standardDeviation(arr) {
}
```
# --solutions--
```js
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;
}
```