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

javascript - Confirm a form & display message if form is valid with JQuery -

Retrieving ETA (estimated time of arrival) with Google Distance Matrix API and public transit as transport mode -

ionic framework - Meteor - Error: Failed to execute 'insertBefore' on 'Node' -