Created
September 9, 2015 12:48
-
-
Save erikologic/571cf7e5ebbaa777af1e to your computer and use it in GitHub Desktop.
Javascript: average which accept null values
This file contains hidden or 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
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