Javascript String stringToN(str)

Description

Javascript String stringToN(str)

// Given a string, find all the numbers in the string and return their sum
function stringToN(str) {
  var split = str.split(""),
    sum = 0;/*from   w w w  . ja va2 s. c o m*/

  for (var i = 0; i < split.length; i++) {
    if (isNumeric(split[i])) {
      var num = parseInt(split[i]);
      sum += num;
    }
  }

  return sum;
}



PreviousNext

Related