diff options
Diffstat (limited to 'utils')
-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; +} |