Everything is running smoothly.
function validUserNames(array) {
return array.filter(function(el) { return el.length < 10 } );
}
console.log(validUserNames(['foo', 'bar', 'baz', 'foobarbazzz']));
Simply inserting a return statement inside the filter function resolves the issue of it returning undefined
. The filter
function necessitates a predicate function, which must return true or false. Items are included in the result when true and excluded when false. The presence of undefined
seems to disrupt this process.
A more succinct syntax can be employed by utilizing an arrow function without curly braces.
For example:
return array.filter(el => el.length < 10);
If you introduce curly braces back into the equation (such as when your function spans multiple lines), the return statement must be explicitly provided.
For instance:
return array.filter(el => { return el.length < 10 });