Javascript: How can I loop through a multi-digit number and find the consecutive digit ^ n of each? -
for instance, let's have number 345. how can in javascript loop through each digit in number , raise consecutive nth power: i.e. 3^1 + 4^2 + 5^3?
this converts number string splits digits raises each power of index plus 1 , reduces via addition result in answer:
('' + 345).split('').map(function(v, i) { return math.pow(parseint(v), i+1) }).reduce(function(a, v) { return + v }, 0)
results in 144
Comments
Post a Comment