Sum Up Numbers
Description
CodeMaster has just returned from shopping. He scanned the check of the items he bought and gave the resulting string to Ratiorg to figure out the total number of purchased items. Since Ratiorg is a bot he is definitely going to automate it, so he needs a program that sums up all the numbers which appear in the given input.
Help Ratiorg by writing a function that returns the sum of numbers that appear in the given inputString
.
Example
For inputString = "2 apples, 12 oranges"
, the output should be
sumUpNumbers(inputString) = 14
.
Input/Output
-
[execution time limit] 4 seconds (js)
-
[input] string inputString
Guaranteed constraints:
6 ≤ inputString.length ≤ 60
.
- [output] integer
[JavaScript (ES6)] Syntax Tips
1
2
3
4
5
6
// Prints help message to the console
// Returns a string
function helloWorld(name) {
console.log("This prints to the console when you Run Tests");
return "Hello, " + name;
}
Solution
1
2
3
function sumUpNumbers(inputString) {
return (inputString.match(/\d+/g)||[]).map(Number).reduce((a,b)=>a+b,0)
}