Saturday, 15 January 2011

javascript - Reduce and Sum Array positions -



javascript - Reduce and Sum Array positions -

lets have array:

var likes = [4,7,3,11,3,73,78,34,7,13];

however, i'm capable of utilize length of 5 due restrictions. want "convert" array lenght of 5, , sum other positions, distribute them equally.

does have clue how can this?

maybe solve problem:

var likes = [4,7,3,11,3,73,78,34,7,13], result_array = [ ]; for(var = 0; < likes.length; i++) { if(i % 2) { var new_val = likes[i - 1] + likes[i]; result_array.push(new_val); } }

see working jsfiddle here.

this cause new array contain [ 11, 14, 76, 112, 20 ], , increment in length when new values added initial array.

javascript

No comments:

Post a Comment