Created
December 16, 2010 23:47
-
-
Save omarish/744238 to your computer and use it in GitHub Desktop.
simple cumulative sum in javascript.
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
cumsum = []; | |
j = [0,1,2,3,4]; | |
for(var a=0;a<j.length;a++) { | |
if(a==0) cumsum[a] = j[0]; | |
else cumsum[a] = cumsum[a-1] + j[a]; | |
} |
How about:
let y=0;
let b=[1,2,3,4,5];
cumsum = b.map(d=>y+=d);
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@ghost's method does not work if you want to use
let
instead ofvar
, because the let-variable gets discarded after the for-loop. Here's my alternative: