Last active
March 23, 2017 00:12
-
-
Save mr21/1a391a7088325f0e4dd7a982bbf4b069 to your computer and use it in GitHub Desktop.
A function to shrink an Array of numbers, example: shrinkArray( [ 10, 20, 200, 250 ], 2 ) -> [ 15, 225 ]
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 shrinkArray( arr, newlen ) { | |
var avg, | |
avglenj, | |
i = 0, | |
j = 0, | |
len = arr.length, | |
avglen = ~~( len / newlen ), | |
newarr = []; | |
for ( ; i < newlen; ++i ) { | |
avg = 0; | |
avglenj = j + avglen; | |
while ( j < avglenj && j < len ) { | |
avg += arr[ j++ ]; | |
} | |
newarr.push( avg / avglen ); | |
} | |
return newarr; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment