-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathnumber_addition.js
43 lines (38 loc) · 1.25 KB
/
number_addition.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/**
* Have the function numberAddition(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.
*
* https://www.coderbyte.com/results/bhanson:Number%20Addition:JavaScript
*
* @param {string} str
* @return {number}
*/
function numberAddition(str) {
const DIGITS = '0123456789';
let numbers = [];
// First find numbers
for (let i = 0, number = ''; i < str.length; i++) {
if (!DIGITS.includes(str[i])) {
if (number !== '') {
numbers.push(number);
}
number = '';
} else {
number += str[i];
// Special case for last char
if (i === str.length - 1) {
numbers.push(number);
}
}
}
let sum = 0;
for (let i = 0; i < numbers.length; i++) {
sum += parseInt(numbers[i]);
}
return sum;
}
module.exports = numberAddition;