-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathEasy - 25 - Number Addition.js
More file actions
32 lines (29 loc) · 972 Bytes
/
Easy - 25 - Number Addition.js
File metadata and controls
32 lines (29 loc) · 972 Bytes
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
// Step By Step
function NumberAddition(str) {
// First we charge our input string into an array by splitting it at every alphabetic character.
// Note that this regex expression will match any capital or lowercase letter.
str = str.split(/[a-zA-Z]/);
// Next, we declare a sum variable to hold our answer.
var sum = 0;
// After that, we iterate over each element in our modified array...
for (var i = 0; i < str.length; i++) {
// ...and if it isn't an empty string...
if (str[i] != "") {
// ...we parse its value with parseInt and add it to our answer.
sum += parseInt(str[i]);
}
}
// Finally, we return our answer
return sum;
}
// Without Comments
function NumberAddition(str) {
str = str.split(/[a-zA-Z]/);
var sum = 0;
for (var i = 0; i < str.length; i++) {
if (str[i] != "") {
sum += parseInt(str[i]);
}
}
return sum;
}