sum of the digits of a number javascript

后端 未结 4 858
无人及你
无人及你 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 13:44

    i use it :)

    var result = eval('123456'.replace(/(\d)(?=\d)/g, '$1+'));
    alert(result); // 21

    without eval

    var result = '123456'.split('').reduce(function(a,b){ return +a+ +b; });
    alert(result); // 21

提交回复
热议问题