How to find sum of integers in a string using JavaScript

后端 未结 3 1320
小蘑菇
小蘑菇 2021-01-29 06:43

I created a function with a regular expression and then iterated over the array by adding the previous total to the next index in the array.

My code isn\'t working. Is m

3条回答
  •  滥情空心
    2021-01-29 07:10

    function sumofArr(str) {
     var tot = str.replace(/\D/g,'').split('');
       return  tot.reduce(function(prev, next) {
       return parseInt(prev, 10) + parseInt(next, 10);
    });}
    

    sumofArr("12sf0as9d");

提交回复
热议问题