RMS Value Of Array in JavaScript Last Updated : 03 Jan, 2023 Comments Improve Suggest changes Like Article Like Report 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 is given below: RMS = \sqrt{\frac{{a_{1}}^{2} + {a_{2}}^{2} + {a_{3}}^{2} + {a_{4}}^{2} + ... + {a_{N}}^{2}}{N}} To calculate the RMS value of an array, first need to square all the elements in the array. Then, take the mean of those squared values by summing it and dividing it by the number of the elements and finally take the square root of that number. Example: In this example, we will find out the root mean square value of the elements of an array. javascript <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); console.log(Rms); </script> Output: 6 Now the above script is converted into a single-line script. To write the above script into a single line, first, convert the arr to another array of squares using the Map function which returns an array. Now apply to reduce on that array directly which will return a single value (sum of all the squares). Sum of the square of the number divided by the number of elements and get the square root. It will produce RMS value. Example 2: In this example, we will write a single-line function for RMS. javascript <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); console.log(RMS); </script> Output: 6 Benefits Of Writing 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 the website involving One-line functions will respond much more quickly as compared to the normal functions. Comment More infoAdvertise with us Next Article RMS Value Of Array in JavaScript imdhruvgupta Follow Improve Article Tags : Algorithms Technical Scripter JavaScript Web Technologies DSA Technical Scripter 2018 javascript-math JavaScript-DSA +4 More Practice Tags : Algorithms Similar Reads JavaScript Program to Print Multiplication Table of a Number We are given a number n as input, we need to print its table. Below are the different approaches to print multiplication table in JavaScriptJavaScript Program to print multiplication table of a number1. Using a for LoopThis is the most common way to print the multiplication table. A for loop repeats 3 min read What is the use of Math object in JavaScript ? The Math object in JavaScript provides a set of methods and properties for mathematical constants and functions. It is a built-in object that allows for performing mathematical operations and accessing mathematical constants without creating an instance.What is the Math Object?The Math object is an 4 min read How to get median of an array of numbers in JavaScript ? In this article, we will see how to find the median of an array using JavaScript. A median is a middle value in a given set of numbers or data. Examples: Input arr1 : [1 4 7 9]Output : 5.5Explanation : The median of the two sorted array is the middle elements which is 5 if the arrayLength =arr1.leng 3 min read How to calculate greatest common divisor of two or more numbers/arrays in JavaScript ? In this article, we are given two or more numbers/array of numbers and the task is to find the GCD of the given numbers/array elements in JavaScript. Examples: Input : arr[] = {1, 2, 3} Output : 1 Input : arr[] = {2, 4, 6, 8} Output : 2 The GCD of three or more numbers equals the product of the prim 2 min read How to test a value x against predicate function and returns fn(x) or x in JavaScript ? In this article, we are given a value x, and the task is to check the value against the predicate function. If the value satisfies the condition or predicate return fn(x) else return x. Predicate functions are the function that takes one item as input and returns true or false based on the whether i 3 min read How to Flatten a Given Array up to Specified Depth in JavaScript? Flatten an array in JavaScript is a common task when dealing with nested arrays. The concept of "flatten" means to convert a multi-dimensional array into a single-dimensional array. When you need to control the depth of flattening, JavaScript provides methods to handle this efficiently. This process 2 min read How to get the standard deviation of an array of numbers using JavaScript ? Given an array and the task is to calculate the standard deviation using JavaScript. Example: Input: [1, 2, 3, 4, 5]Output: 1.4142135623730951Input: [23, 4, 6, 457, 65, 7, 45, 8]Output: 145.13565852332775Please refer to Mean, Variance, and Standard Deviation for details. Mean is average of element. 3 min read How to evaluate binomial coefficient of two integers n and k in JavaScript ? The following are the common definitions of Binomial Coefficients. A binomial coefficient C(n, k) can be defined as the coefficient of x^k in the expansion of (1 + x)^n. Binomial coefficient C(n, k) also gives the number of ways, disregarding order, that k objects can be chosen from among n objects 2 min read How to check two numbers are approximately equal in JavaScript ? In this article, we are given two numbers and the task is to check whether the given numbers are approximately equal to each other or not. If both numbers are approximately the same then print true otherwise print false. Example: Input: num1 = 10.3 num2 = 10 Output: true Approach: To check whether t 2 min read How to create slider to map a range of values in JavaScript ? The task is to map a range of values to another range of values like map (0-100) to (100-10000000) in JavaScript. There are two approaches that are discussed below.Approach 1: Use the logarithmic scale to map the range. In this example, first, the log value of the range is calculated and then the sc 2 min read Like