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;
}
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