-3

I'm trying to use the parseInt to show only numbers from the following array:

1,2,a,b

Here is my javascript code so far:

var filter_list = ["1,2,a,b"];

function myFunction() {
  parseInt(filter_list);
  return filter_list;
}

document.getElementById("display").innerHTML = filter_list;

Maybe my idea isn't even going to work. Would love some feedback.

Eddie
  • 26,593
  • 6
  • 36
  • 58
Brent Nicolet
  • 345
  • 1
  • 5
  • 9
  • 1
    It seems like your missing some foundational knowledge of JavaScript here. You haven't got an array of four items, you have a single item array containing a string. You define a function (`myFunction`) but don't invoke it anywhere, and that function uses `parseInt` but doesn't consume the return value. You should probably start with a JavaScript tutorial that covers some of these basic concepts, there are some good ones on MDN: https://developer.mozilla.org/bm/docs/Web/JavaScript – user229044 Jun 23 '18 at 15:17
  • You need to call parseInt for every element, not for the whole array. Then you can filter for NaN – K.. Jun 23 '18 at 15:17

2 Answers2

0

You have array with one element that is string, so you could use join and split and then filter method.

var data = ["1,2,a,b"];
var numbers = data.join(",").split(",").filter(Number);
console.log(numbers)
Nenad Vracar
  • 118,580
  • 15
  • 151
  • 176
0

First, you have to know that each element of the array has to have teir own " ". Like that:

var filter_list = ["1","2","a","b"];

then, if you want to know the elements that are numbres, you can do something like that:

var filter_list = ["1","2","a","b"];

for(i = 0; i < filter_list.length; i++ {

    if (!isNaN(ParseInt(filter_list[i])){

       var new_array = [];

       new_array[i] = filter_list[i];
    }
}

document.getElementById("display").innerHTML = new_array;

I don't know if is that what you wanted to know.

Diego Saravia
  • 162
  • 11