-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNumberSearch.js
More file actions
62 lines (40 loc) · 1.43 KB
/
NumberSearch.js
File metadata and controls
62 lines (40 loc) · 1.43 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*
Using the JavaScript language, 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 "55Hello"
and "5Hello 5" should return two different answers. Each string will contain
at least one letter or symbol.
*/
function NumberAddition(str) {
var totalSum = 0;
var currentNum = 0;
for (var i = 0; i < str.length; i++) {
if (str[i].match(/[0-9]/)){
currentNum = parseInt(str[i]);
//console.log("currentNum: " + currentNum);
if (str[i+1] && str[i+1].match(/[0-9]/)) {
remainingStr = str.substring(i+1);
//console.log("remainingStr: " + remainingStr);
if (remainingStr[0].match(/[0-9]/)) {
//console.log('matched');
nextNum = parseInt(remainingStr[0]);
currentNum = currentNum * 10 + nextNum;
//console.log("currentNum: " + currentNum);
totalSum += currentNum;
//console.log("totalSum: " + totalSum);
i++;
}
} else {
totalSum += currentNum;
//console.log("totalSum: " + totalSum);
}
}
}
return totalSum;
}
NumberAddition("75Number9"); //output = 84
//NumberAddition("10 2One Number*1*"); // output = 13
//NumberAddition();
//NumberAddition();