javascript - Adding numbers together -
i want loop on array whilst addding numbers together.
whilst looping on array, add current number next.
my array looks
[0,1,0,4,1]
i following;
[0,1,0,4,1] - 0+1= 1, 1+0= 1, 0+4=4, 4+1=5
which give me [1,1,4,5]
following; 1+1 = 2, 1+4=5, 4+5=9
and on until 85.
could advise on best way go
this transform follows specified method of summation, end result of 21, please specify how 85.
var ary = [0,1,0,4,1], transform = function (ary) { var length = ary.length; return ary.reduce(function (acc, val, index, ary) { if (index + 1 !== length) acc.push(ary[index] + ary[index + 1]); return acc; }, []); }; while (ary.length !== 1) ary = transform(ary);
Comments
Post a Comment