我使用正则表达式创建了一个函数,然后通过将前一个总数添加到数组中的下一个索引来迭代数组。
我的代码不起作用。我的逻辑有问题吗?忽略语法
function sumofArr(arr) { // here i create a function that has one argument called arr
var total = 0; // I initialize a variable and set it equal to 0
var str = "12sf0as9d" // this is the string where I want to add only integers
var patrn = \\D; // this is the regular expression that removes the letters
var tot = str.split(patrn) // here i add split the string and store it into an array with my pattern
arr.forEach(function(tot) { // I use a forEach loop to iterate over the array
total += tot; // add the previous total to the new total
}
return total; // return the total once finished
}