From a176c8d5ee2e022e749bba234d597f0725ea8476 Mon Sep 17 00:00:00 2001 From: Holden Rohrer Date: Fri, 27 Dec 2019 16:35:16 -0500 Subject: added array normalizing util --- utils/norm.js | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 utils/norm.js (limited to 'utils') 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; +} -- cgit