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

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -