Created
January 19, 2014 19:01
-
-
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 "…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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; | |
} |
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
Plzz give me full code