const sumOfNumberInString = (s) =>{
let temp = "0";
let sum = 0;
for (let i = 0; i < str.length; i++) {
let ch = str[i];
if (!isNaN(String(ch) * 1))
temp += ch;
else {
sum += parseInt(temp);
temp = "0";
}
}
return sum + parseInt(temp);
}
sumOfNumberInString("1aba23")
For further actions, you may consider blocking this person and/or reporting abuse
Top comments (1)
Here is my version
const sumOfNumberInString= (str)=>{
const reg = /(\d+)/g;
return [...str.matchAll(reg)].reduce((acc, cur)=> acc + parseInt(cur[0]), 0);
}