Skip to content

Instantly share code, notes, and snippets.

@totty90
Last active February 1, 2017 12:48
Show Gist options
  • Save totty90/b58f47dbc430a53d2e5b to your computer and use it in GitHub Desktop.
Save totty90/b58f47dbc430a53d2e5b to your computer and use it in GitHub Desktop.
Excel weightedAverage function
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