diff options
author | Holden Rohrer <holden.rohrer@gmail.com> | 2019-12-27 16:35:16 -0500 |
---|---|---|
committer | Holden Rohrer <holden.rohrer@gmail.com> | 2019-12-27 16:35:16 -0500 |
commit | a176c8d5ee2e022e749bba234d597f0725ea8476 (patch) | |
tree | c1568d7e963ea53ad19dd8947da6882b428d6323 | |
parent | 22dbbd1e651743da8c533f2fb4a92ad681c46f67 (diff) |
added array normalizing util
-rw-r--r-- | utils/norm.js | 8 |
1 files changed, 8 insertions, 0 deletions
diff --git a/utils/norm.js b/utils/norm.js new file mode 100644 index 0000000..a1a4fb3 --- /dev/null +++ b/utils/norm.js @@ -0,0 +1,8 @@ +// Normalizes an array of numbers such that they sum to one + +module.exports = function(arr){ // Modifies arr + let sum = arr.reduce( (acc, cur) => arr+cur ); + for (elem of arr) + elem = elem/sum; + return arr; +} |