2018-09-30 23:01:58 +01:00
---
2019-07-29 07:14:15 -07:00
title: Averages/Root mean square
2018-09-30 23:01:58 +01:00
id: 594da033de4190850b893874
challengeType: 5
2019-08-05 09:17:33 -07:00
forumTopicId: 302228
2018-09-30 23:01:58 +01:00
---
## Description
< section id = 'description' >
2019-03-01 17:10:50 +09:00
Compute the < a href = "https://en.wikipedia.org/wiki/Root mean square" title = "wp: Root mean square" target = '_blank' > Root mean square< / a > of the numbers 1 through 10 inclusive.
2019-06-14 20:04:16 +09:00
The < i > root mean square< / i > is also known by its initials RMS (or rms), and as the < strong > quadratic mean< / strong > .
2019-02-25 13:36:09 +09:00
The RMS is calculated as the mean of the squares of the numbers, square-rooted:
< big > $$x_{\mathrm{rms}} = \sqrt {{{x_1}^2 + {x_2}^2 + \cdots + {x_n}^2} \over n}. $$</ big >
2018-09-30 23:01:58 +01:00
< / section >
## Instructions
< section id = 'instructions' >
< / section >
## Tests
< section id = 'tests' >
```yml
2018-10-04 14:37:37 +01:00
tests:
2019-11-20 07:01:31 -08:00
- text: < code > rms</ code > should be a function.
2019-07-26 05:24:52 -07:00
testString: assert(typeof rms === 'function');
2018-10-20 21:02:47 +03:00
- text: < code > rms([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])</ code > should equal < code > 6.2048368229954285</ code > .
2019-07-26 05:24:52 -07:00
testString: assert.equal(rms(arr1), answer1);
2018-09-30 23:01:58 +01:00
```
< / section >
## Challenge Seed
< section id = 'challengeSeed' >
< div id = 'js-seed' >
```js
2019-02-26 17:07:07 +09:00
function rms(arr) {
2020-09-15 09:57:40 -07:00
2018-09-30 23:01:58 +01:00
}
```
< / div >
### After Test
< div id = 'js-teardown' >
```js
2018-10-20 21:02:47 +03:00
const arr1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const answer1 = 6.2048368229954285;
2018-09-30 23:01:58 +01:00
```
< / div >
< / section >
## Solution
< section id = 'solution' >
```js
2019-02-26 17:07:07 +09:00
function rms(arr) {
2018-09-30 23:01:58 +01:00
const sumOfSquares = arr.reduce((s, x) => s + x * x, 0);
return Math.sqrt(sumOfSquares / arr.length);
}
```
< / section >