Skip to content

Instantly share code, notes, and snippets.

@areagray
Created January 19, 2014 19:01
Show Gist options
  • Save areagray/8509371 to your computer and use it in GitHub Desktop.
Save areagray/8509371 to your computer and use it in GitHub Desktop.
Coderbyte: 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 "…
//coderbyte
function NumberAddition(str) {
var mathches,
total=0;
// use regexp to grab the numbers
matches=str.match(/([\d]+)/g);
//itereate and add
for (var i = 0; i<matches.length;i++){
total = total + parseInt(matches[i]);
}
return total;
}
@rashmi3529
Copy link

Plzz give me full code

@TheJhoNY75
Copy link

i tink is better like that:

function NumberAddition(str) {
  return str.match(/([\d]+)/g).reduce((a, b) => {
    return parseInt(a) + parseInt(b)
  })
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment