Skip to content

Instantly share code, notes, and snippets.

@rvbsanjose
Last active August 29, 2015 13:59
Show Gist options
  • Save rvbsanjose/10813404 to your computer and use it in GitHub Desktop.
Save rvbsanjose/10813404 to your computer and use it in GitHub Desktop.
// Using the JavaScript language, have the function NumberSearch(str) take the str parameter, search for all the numbers in the
// string, add them together, then return that final number. For example: if str is "88Hello 3World!" the output should be 91.
// You will have to differentiate between single digit numbers and multiple digit numbers like in the example above. So
// "55Hello" and "5Hello 5" should return two different answers. Each string will contain at least one letter or symbol.
// Input = "75Number9" Output = 84
// Input = "10 2One Number*1*" Output = 13
function numCoverter( str ) {
var sum = 0;
while ( str.match( /\d+/ ) ) {
var current = parseInt( str.match( /\d+/ )[0] );
sum += current;
str.replace( /\d+/, '' );
}
return sum;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment