Q:

Write a JavaScript program to compute the sum of all digits that occur in a given string

0

Write a JavaScript program to compute the sum of all digits that occur in a given string.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

function sum_digits_from_string(dstr) {
  var dsum = 0;

  for (var i = 0; i < dstr.length; i++)
  {

    if (/[0-9]/.test(dstr[i])) dsum += parseInt(dstr[i])
  }
  return dsum;
}

console.log(sum_digits_from_string("abcd12efg9"))
console.log(sum_digits_from_string("w3resource"))

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now