Skip to content

Commit aae08c6

Browse files
committed
Create Number Addition
1 parent 2101b30 commit aae08c6

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

Number Addition

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/***************************************************************************************
2+
* *
3+
* CODERBYTE BEGINNER CHALLENGE *
4+
* *
5+
* Number Addition *
6+
* Using the JavaScript language, have the function NumberSearch(str) take the str *
7+
* parameter, search for all the numbers in the string, add them together, then *
8+
* return that final number. For example: if str is "88Hello 3World!" the output *
9+
* should be 91. You will have to differentiate between single digit numbers and *
10+
* multiple digit numbers like in the example above. So "55Hello" and "5Hello 5" *
11+
* should return two different answers. Each string will contain at least one letter *
12+
* or symbol. *
13+
* *
14+
* SOLUTION *
15+
* I only want numbers in the string so I am using RegExp to remove everything that *
16+
* is not a number. Then convert that to an array. Loop thru each number in the array *
17+
* and add tot to get the answer. *
18+
* *
19+
* Steps for solution *
20+
* 1) Initialize tot to zero *
21+
* 2) Remove everything but numbers from string and convert to array *
22+
* 3) Loop thru each number in array and add to tot *
23+
* 4) Return tot for answer *
24+
* *
25+
***************************************************************************************/
26+
27+
function NumberAddition(str) {
28+
29+
var tot = 0;
30+
31+
str = str.replace(/[^0-9\.]+/g," ").split(" ");
32+
for (var i = 0; i < str.length; i++) {
33+
tot += Number(str[i]);
34+
}
35+
36+
return tot;
37+
38+
}

0 commit comments

Comments
 (0)