Skip to content

Commit 5a87142

Browse files
adding get_rms_value function
1 parent dbab3c4 commit 5a87142

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

README.md

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ In your `package.json` add the following, `"type": "module"`.
2020
# Example Usage
2121

2222
```js
23-
import { is_array, object_to_array, search_in_array,sanitize_array } from "@hetarth02/js-array-helpers";
23+
import { is_array, object_to_array, search_in_array,sanitize_array, get_rms_value } from "@hetarth02/js-array-helpers";
2424

2525
let arr = [1, 2];
2626
console.log(is_array(arr)); // true
@@ -69,4 +69,16 @@ console.log(sanitize_array(my_array,my_schema));
6969
// { name: 'sam', age: 123, isEmployed: false }
7070
// ]
7171

72-
```
72+
73+
// get_rms_value example
74+
75+
// Given array of numbers
76+
const values = [23, 54, 19];
77+
78+
// Run get_rms_value with array
79+
console.log(get_rms_value(values))
80+
81+
// Calculated Root Mean Square value
82+
// 35.61834733205159
83+
84+
```

index.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,18 @@ function get_only(arr, keys) {
353353
);
354354
}
355355

356+
/**
357+
* To get RMS (Root Mean Square) Value.
358+
*
359+
* @param array arr
360+
* @returns number
361+
*/
362+
function get_rms_value(arr) {
363+
is_array(arr);
364+
let sum_square = arr.reduce((ss, curr) => (isNaN(curr) ? 0 : ss + curr*curr), 0);
365+
return Math.sqrt(sum_square/arr.length);
366+
}
367+
356368
export {
357369
is_array,
358370
is_num_array,
@@ -374,4 +386,5 @@ export {
374386
difference,
375387
sanitize_array,
376388
get_only,
389+
get_rms_value,
377390
};

0 commit comments

Comments
 (0)