The RMS (Root mean square) value of a distribution is the square root of the mean of the squares of the elements. The formula to find RMS value are given below:

To calculate RMS value of an array, first need to square all the elements in the array. Then, take the mean of those squared value by summing it and dividing it by the number of the elements and finally take the square root of that number.

**Example:**

`<script> ` ` ` `let CalculateRMS = ` `function` `(arr) { ` ` ` ` ` `// Map will return another array with each ` ` ` `// element corresponding to the elements of ` ` ` `// the original array mapped according to ` ` ` `// some relation ` ` ` `let Squares = arr.map((val) => (val*val)); ` ` ` ` ` `// Function reduce the array to a value ` ` ` `// Here, all the elements gets added to the first ` ` ` `// element which acted as the accumulator initially. ` ` ` `let Sum = Squares.reduce((acum, val) => (acum + val)); ` ` ` ` ` `Mean = Sum/arr.length; ` ` ` `return` `Math.sqrt(Mean); ` `} ` ` ` `let arr = [5, 9, 3, -7, -4]; ` ` ` `let Rms = CalculateRMS(arr); ` ` ` `document.write(Rms); ` `</script> ` |

*chevron_right*

*filter_none*

**Output:**

6

Now the above script converted into single line script. To write the above script into single line, first convert the arr to another array of squares using Map function which returns an array. Now apply reduce on that array directly which will return a single value (sum of all the squares). Sum of square of number divided by number of elements and get the square root. It will produce RMS value.

**Example:**

`<script> ` `let CalculateRMS = (arr) => Math.sqrt( ` ` ` `arr ` ` ` `.map( val => (val * val)) ` ` ` `.reduce((acum, val) => acum + val) ` ` ` `/arr.length ` `); ` ` ` `// The above can be written without any ` `// line breaks in between as a single-line. ` `// For the sake of easy ` `// understanding it is written like that. ` ` ` `let arr = [5, 9, 3, -7, -4]; ` `let RMS = CalculateRMS(arr); ` `document.write(RMS); ` `</script> ` |

*chevron_right*

*filter_none*

**Output:**

6

**Benefits Of Writing An One-line Functions**

The less code will require less memory, so it will take less time for any Web Browser to load the script with less memory. Further, the browser renders the WebPage by executing the code line-by-line, so any operation on website involving One-line functions will respond much quickly as compared to the normal functions.

## Recommended Posts:

- How to convert a pixel value to a number value using JavaScript ?
- Sort array of objects by string property value in JavaScript
- Max/Min value of an attribute in an array of objects in JavaScript
- How to convert an Object {} to an Array [] of key-value pairs in JavaScript?
- How to store a key=> value array in JavaScript ?
- JavaScript TypeError - Reduce of empty array with no initial value
- How to compare two JavaScript array objects using jQuery/JavaScript ?
- How to get hex color value of RGB value ?
- How to change an element color based on value of the color picker value using onclick?
- How to push a value based on matched value in PHP ?
- How to change an element color based on value of the color picker value on click?
- How to get the value of text input field using JavaScript?
- JavaScript | Know the value of GET parameters from URL
- How to set the value of a select box element using JavaScript?
- How to add key/value pair to a JavaScript object?
- How to access first value of an object using JavaScript ?
- How to get value of selected radio button using JavaScript?
- Set the value of an input field in JavaScript
- How to get selected value in dropdown list using JavaScript ?
- How to convert a currency string to a double value with jQuery or Javascript?

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.