sum of the digits of a number javascript

后端 未结 4 870
无人及你
无人及你 2021-01-04 13:07

I saw a bunch of other posts on this topic but none in javascript. here is my code.

var theNumber = function digitAdd (base, exponent) {
    var number = 1;
         


        
4条回答
  •  灰色年华
    2021-01-04 14:01

    i use this :

    '123456'.split('').map(function(e){return parseInt(e)}).reduce(function(a,b){return a+b}); //21
    

    Update (ES6 syntax) :

    [...'123456'].map(e=>parseInt(e)).reduce((a,b)=>a+b); //21
    

提交回复
热议问题