I'm stuck with one task, hope you could help me
I have to return an combined array which consist only from numbers. So if the element has got except for number another character, such element should be removed.
I had few ideas. Unfortunately, none of them work properly. providing my code
function combineArray(arr1, arr2) {
let array1 = filt(arr1);
let array2 = filt(arr2);
let arr3 = array1.concat(array2);
return arr3;
}
function filt(array){
array.forEach(function(item,index) {
if (typeof item !== 'number'){
array.splice(index, 1);
}
})
return array;
}
Here's a simple solution that uses the filter
method on arrays and Number.isFinite
to check if a variable is a number:
function combineArray(arr1, arr2) {
let array1 = filt(arr1);
let array2 = filt(arr2);
let arr3 = array1.concat(array2);
return arr3;
}
function filt(array) {
return array.filter(x => Number.isFinite(x));
}
console.log(combineArray(['item1', 2, 3, 'item4'],['1','second','3rd',4]));
This outputs: [2, 3, 4]