I am trying to solve a problem I'm facing in Javascript, I am totally new to JS. This is my code:
function filterOutStringfromArr(array) {
var arr = []
for (var i = 0; i < array.length; i++) {
if (typeof array[i] === 'number') {
arr[i] = array[i]
}
}
console.log(arr)
}
filterOutStringfromArr([1,2,'A','B',123])
when I run this, I get this result: [ 1, 2, <2 empty items>, 123 ].
I know this is happening because the length of my array is 5. But I want to append just the filtered value to the empty array arr[]. How can I do this?
arr.push()instead ofarr[i] =.I want to append just the filtered value to the empty arrayTo me that reads add the found item to the last element in the array. Are you looking for something different?