Skip to content

Instantly share code, notes, and snippets.

@erikologic
Created September 9, 2015 12:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erikologic/571cf7e5ebbaa777af1e to your computer and use it in GitHub Desktop.
Save erikologic/571cf7e5ebbaa777af1e to your computer and use it in GitHub Desktop.
Javascript: average which accept null values
16 lines (14 sloc) 0.36 kB
function average(data) {
/*Can't find an average function in JS, made one
This function is able to handle null values!!!*/
var count = null;
var sum = null;
for (i=0; i<data.length; i++) {
if (data[i]) {
count++;
sum = sum + data[i];
}
}
if (count) {var average = Number( sum / count)} else { average=null}
return average;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment