#Javascript #JavaScript #Codewars

I cant understand this solution.Can someone explain it for me!?
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;
}

Hi @Leebal13

Welcome to the FCC forum. Looks like you misunderstood how Array.prototype.filter works.

According to the documentation,

The callback function returns a value that coerces to true to keep the element, or to false otherwise.

Take note, 0 is not a truthy value.

1 Like

In JavaScript 0 is equal to false because “0” is of type string but when tested for equality the automatic type conversion of JavaScript comes into effect and converts the “0” to its numeric value which is 0 , which is false

You might need to manually add a conditional statement to check for 0

1 Like

@nibble thank you for your help.I am just started learning so same methods make me confuse.

Thank you for your explanation. I didn’t know it before.@Exotic