Last active
February 1, 2017 12:48
-
-
Save totty90/b58f47dbc430a53d2e5b to your computer and use it in GitHub Desktop.
Excel weightedAverage function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function weightedAverage(v, w) { | |
var sum_v = 0; | |
var sum_w = 0; | |
if(!v || !w){ | |
return "#ERROR: You need 2 arguments."; | |
} | |
if (v.length != w.length) { | |
return "#ERROR: Incorrect number of values and weights"; | |
} | |
var i = v.length; | |
while(i--) { | |
if(isNaN(parseFloat(v[i]))) continue; | |
sum_v += parseFloat(v[i]) * parseFloat(w[i]); | |
sum_w += parseFloat(w[i]); | |
} | |
return sum_v/sum_w; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment