Create a function that takes a list of non-negative integers and strings and returns a new list with the strings filtered out. When I pass [1,'a','b',0,15]
, it returns [1,15]
instead of [1,0,15]
. Please suggest any corrections.
function filter_list(l) {
// Return a new array with the strings filtered out
var filt = l.filter(function(x) {
if (typeof(x) === 'number')
return x;
});
return filt;
}
Create a function that takes a list of non-negative integers and strings and returns a new list with the strings filtered out. When I pass [1,'a','b',0,15]
, it returns [1,15]
instead of [1,0,15]
. Please suggest any corrections.
function filter_list(l) {
// Return a new array with the strings filtered out
var filt = l.filter(function(x) {
if (typeof(x) === 'number')
return x;
});
return filt;
}
Share
Improve this question
edited Jul 9, 2018 at 7:43
Priya
3343 silver badges8 bronze badges
asked Jul 9, 2018 at 5:17
Nikita Nikita
2115 silver badges16 bronze badges
6 Answers
Reset to default 40 is falsey, and if the value returned by the filter
callback is falsey, the item being iterated over will not be included in the final array. Simply return typeof x === 'number'
instead:
function filter_list(l) {
return l.filter(x => typeof x === 'number');
}
console.log(filter_list([1, 'a', 'b', 0, 15]));
You are returning x
instead of the result of the boolean typeof(x) === 'number'
. x
is 0 which is falsy so it gets filtered out.
There are several falsy values in javascript. For instance this filter returns an empty array:
console.log([0, '', 'A'/2, null, undefined].filter(x => x))
Instead, return the result of the test directly from the filter
callback:
function filter_list(l) {
// Return a new array with the strings filtered out
return l.filter(x => typeof x === 'number')
}
console.log(filter_list([1, 'a', 'b', 0, 15]));
0 is considered as false. return true or false from filter function as below.
function filter_list(l) {
// Return a new array with the strings filtered out
return l.filter(function(x) {
if (typeof(x) === 'number')
return true;
});
}
console.log(filter_list([1, 'a', 'b', 0, 15]));
A filter function accepts the boolean value to return.
Now take look for your code.
if (typeof(x) === 'number')
return x;
As per your code return x will convert into return 0 and finally filter will understand returned value as false. Because 0 bees false.
So, you can use arrow function:
function filter_list(l) {
return l.filter(x => typeof x === 'number');
}
Or,
function filter_list(l) {
return l.filter(function(x) {
if (typeof(x) === 'number')
return true;
});
}
package ArrayPractice;
import java.util.*;
public class Question8 {
public static ArrayList<Integer> dataguise= new ArrayList<>();
public void filterArray(Object[] data){
for(int i=0;i<data.length;i++){
if(data[i] instanceof Integer){
dataguise.add(i);
}}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the array range: ");
int i= sc.nextInt();
int [] data= new int[i];
Object [] demo= new Object[i];
for (int y = 0; y < data.length; y++) {
System.out.println("Enter the Array number: " + y);
if(sc.hasNextInt()){
int value= sc.nextInt();
dataguise.add(value);
}else{
demo[y]= sc.next();
}
}
Question8 question8= new Question8();
question8.filterArray(demo);
for(int v: dataguise){
System.out.println(v);
}
}
}
This is a short way to write the it.
function filter_list(l)
{
return l.filter(Number.isInteger);
}