1

How do I calculate the sum of all the numbers in a string by using the JavaScript ? In the example below, the expected result would be 2+0+1+5. My attempt is below.

var a = "weare20boysand15girls";
    var i;
    for(i=0; i<a.length; i++){
        var b = Number(a[i]);
        var c=0;
        c += b;
        console.log(c);

7 Answers 7

1

Use regular expression to find all the integers. and use reduce function to compute sum

var a = "weare20boysand15girls";

var res = a.replace(/\D+/g, '').split('').reduce((a, b) => {
  return parseInt(a) + parseInt(b);
});
console.log(res)

Sign up to request clarification or add additional context in comments.

Comments

0

in the question you asked for an expected result of 2015

the given string is

var a = "weare20boysand15girls";

the oneliner for this solution:

console.log([...a].filter( e => isFinite(e)).join(''));    

but you marked as correct answer an result of 8

the oneliner for this solution:

console.log([...a].filter( e => isFinite(e)).reduce((a, b)=>parseInt(a)+parseInt(b)));

Use regex only when it is impossible to use an other way...

2 Comments

Thank you for your valuable answer, but can you please explain it sir ?
yes i can ... the [...a]convert the string to an array. i .filter this array and Check whether a number is isFinite, legal number. the sum function reducemust not be explained (?)
0

You can try :

var a = "weare20boysand15girls";
var b = a.match(/(\d)/g).map((a) => parseInt(a)).reduce((a,b) => a + b);

Comments

0

I would suggest regex solution for this. Separate number from your whole string by digits [0-9] regex and sum up them using loop.

var a = "weare20boysand15girls";
const regex = /[0-9]/g;
var num = a.match(regex);
var result=0;
num.forEach(function(match){
    result+=parseInt(match);
});
console.log(result);

Comments

0

You could

const input = "weare20boysand15girls";
const result =
    // Transform the string to an array.
    input.split("")
    // Use the reduce function to sum only the elements that are numbers.
    .reduce((sum, el) => isNaN(el) ? sum : sum + Number(el), 0);

Comments

0

check the ascii value to identify numerical characters, and then sum it

var sum = 0;
var a = "weare20boysand15girls";
for (var i; i < a.length; i++) {
  if (a.charCodeAt(i) >= 48 && a.charCodeAt(i) <= 57) {
    sum += a.charCodeAt(i) - 48;
  }
}
console.log(sum);

Comments

0

//Try this: You have to split the string to an array and then test each character for addition.

function sumNumbers_in_a_String() {

  var strName = "weare20boysand15girls";
  var strArray = strName.split("");
  var c = 0;
  var l = strArray.length;
  for (var i = 0; i < l; i++){
      if (parseInt(strArray[i])){
         var b = Number(strArray[i]);
         var c = c + b;
      }
 }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.