Reputation: 2692
I would like to loop through an array of numbers and letters and extract only the numbers . I tried using the typeof operator but my code doesn't seems to work
str = "55abcdef6ghij78"
function NumberAddition(str) {
var numArr = [];
str = str.split("");
for (var i = 0; i < str.length; i++) {
if (typeof str[i] === 'number') {
numArr.push(str[i]);
}
return numArr;
}
}
Upvotes: 0
Views: 1338
Reputation: 1698
Try parsing the character as an Integer first, and then check the type.
if (parseInt(str[i])) {
numArr.push(str[i]);
}
As pointed out. typeof NaN
is also number
. So typeof parseInt('a')
won't work.
Upvotes: 1
Reputation: 147373
Some good answers, but the simplest way to me is to remove the non–digits first, then create the array, so:
var str = '55abcdef6ghij78';
str.replace(/\D/g,'').split(''); // ['5','5','6','7','8'];
you want to keep sequences of digits, so match is better:
str.match(/\d+/g); // ["55", "6", "78"]
If you want to sum the numbers, use reduce:
function numberAddition(s) {
return s.match(/\d+/g).reduce(function(prev, curr){return prev + +curr}, 0);
}
console.log(numberAddition(str)); // 139
noting that a for loop will usually be faster, but not enough to matter unless you're doing this a lot or with very big arrays of numbers.
PS. Don't forget to declare variables, and function names starting with a capital letter are, by convention, reserved for constructors.
Upvotes: 1
Reputation: 239463
If you want only the numbers, then there are so many ways to do it. But the simplest would be using a RegEx,
console.log("55abcdef6ghij78".match(/\d+/g).join(""));
// 55678
This finds all the consecutive digits (one or more digits) in the string and returns them as an array and we finally join everything together to get a single string.
If you want all the numbers to be separate elements in the array, then you can tweak the RegEx a little bit, like this
console.log("55abcdef6ghij78".match(/\d/g));
// [ '5', '5', '6', '7', '8' ]
If you want to group the numbers, as you asked for in the comments then just don't join them
console.log("55abcdef6ghij78".match(/\d+/g));
// [ '55', '6', '78' ]
If you want to convert them to numbers as soon as you group them, you can map
the result array to Number
function, like this
console.log("55abcdef6ghij78".match(/\d+/g).map(Number));
// [ 55, 6, 78 ]
Apart from that, if you want to fix your program, then there two major mistakes.
typeof str[i] === 'number'
.
The elements in str
will already be of type string only. So, they will never be equal to number
. To fix this, you can use the classic trick,
if (+str[i] == str[i])
This converts the current string to a number and compares it with the string value of it. If they are equal, then it means that the current element is a digit.
Apart from that, the return
statement is within the for
loop itself.
So, your program can be fixed like this
function NumberAddition(str) {
var numArr = [];
str = str.split("");
for (var i = 0; i < str.length; i++) {
if (+str[i] == str[i]) {
numArr.push(str[i]);
}
}
return numArr; // Outside the for loop
}
console.log(NumberAddition("55abcdef6ghij78"));
// [ '5', '5', '6', '7', '8' ]
But the idiomatic way to do this would be to use Array.prototype.filter
, like this
function NumberAddition(str) {
return str.split("").filter(function(currentChar) {
return +currentChar == currentChar;
})
}
console.log(NumberAddition("55abcdef6ghij78"));
But, the shorter version would be using Number
, like this
console.log("55abcdef6ghij78".split("").filter(Number));
// [ '5', '5', '6', '7', '8' ]
Upvotes: 3