robosats/frontend/src/utils/weightedMean.ts
KoalaSat 293c0b604d Refactor contexts and models (#921)
* Add SVG icons for map pins

* Add federation basis and new coordinator form (#793)

* Add new coordinator entry issue form

* Add Federation basis

* Fix eslint errors from F2F and fix languages

* Redo eslint @typescript-eslint/strict-boolean-expressions

* Robot Page working

* Contexts Working

* Garage Working

* CurrentOrder working

* Federation model working

---------

Co-authored-by: Reckless_Satoshi <reckless.satoshi@protonmail.com>
Co-authored-by: Reckless_Satoshi <90936742+Reckless-Satoshi@users.noreply.github.com>
2023-12-02 10:40:59 +00:00

17 lines
417 B
TypeScript

export const weightedMean = (arrValues: number[], arrWeights: number[]): number => {
if (arrValues.length === 0) {
return 0;
}
const result = arrValues
.map((value, i) => {
const weight = arrWeights[i];
const sum = value * weight;
return [sum, weight];
})
.reduce((p, c) => [p[0] + c[0], p[1] + c[1]], [0, 0]);
return result[0] / result[1];
};
export default weightedMean;