Skip to content

Commit bc1ebf7

Browse files
authored
Merge pull request #24 from vaibhavraj-exe/adding-get-rms-value-function
adding get_rms_value function
2 parents 8dcd7a3 + b9b3ed5 commit bc1ebf7

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626
object_to_array,
2727
search_in_array,
2828
sanitize_array,
29+
get_rms_value,
2930
} from "@hetarth02/js-array-helpers";
3031

3132
let arr = [1, 2];
@@ -75,6 +76,17 @@ console.log(sanitize_array(my_array, my_schema));
7576
// { name: 'sam', age: 123, isEmployed: false }
7677
// ]
7778

79+
// get_rms_value example
80+
81+
// Given array of numbers
82+
const values = [23, 54, 19];
83+
84+
// Run get_rms_value with array
85+
console.log(get_rms_value(values))
86+
87+
// Calculated Root Mean Square value
88+
// 35.61834733205159
89+
7890
// to reverse an array in parts
7991
let my_array = [1, 2, 3, 4, 5];
8092
let reverseInPart_array = array_reverse_part(my_array, 3, 4);

index.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,18 @@ function array_reverse_part(arr, start, end) {
395395
}
396396

397397
/**
398+
* To get RMS (Root Mean Square) Value.
399+
*
400+
* @param array arr
401+
* @returns number
402+
*/
403+
function get_rms_value(arr) {
404+
is_array(arr);
405+
let sum_square = arr.reduce((ss, curr) => (isNaN(curr) ? 0 : ss + curr*curr), 0);
406+
return Math.sqrt(sum_square/arr.length);
407+
}
408+
409+
/**
398410
* To rotate an array to the left counter-clockwise by x steps, where x is non-negative
399411
*
400412
* @param array arr
@@ -478,4 +490,5 @@ export {
478490
array_reverse_part,
479491
array_rotate,
480492
equilibrium_Point,
493+
get_rms_value,
481494
};

0 commit comments

Comments
 (0)