Learn Modern JavaScript Methods by Building Football Team Cards - Step 41

Tell us what’s happening:

Call the setPlayerCards function with an argument of players.filter().

Inside the filter method, add a callback function with a parameter called player and implicitly return player.nickname is not null.

Your code so far

switch (e.target.value) {
    case "nickname":
      setPlayerCards(players.filter((player) => (player.nickname !== null ? true : false)))
  }

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36

Challenge Information:

Learn Modern JavaScript Methods by Building Football Team Cards - Step 41

You don’t need the ternary.
You can return the comparison here

3 Likes

It worked. Thank you.

2 Likes

If someone would care to explain, I had similar formatting as manav1405 with this problem:

setPlayerCards(players.filter((player) => {player.nickname !== null}))

However with all the braces the system would not accept the answer. Ultimately I had to remove as many braces as possible, resulting in:

setPlayerCards(players.filter(player => player.nickname !== null))

Its a minor problem and hardly important, but could someone explain why this won’t pass with the braces?

Thanks!!

That’s because the directions ask you to use an implicit return. An implicit return means that you don’t have to use explicitly use the return keyword.

This is commonly used in high order functions like map, filter, etc, or regular functions where you are only returning one expression

For example:

const addTwoNumbers = (num1, num2) => num1 + num2

If your function is larger in nature and has many expressions, then you should wrap the function body in curly brace {} and use the return keyword.

In your code here

you are missing the return keyword.

when you rewrote it, then you were using the correct implicit return that the directions asked for.

It is important to be comfortable with both the explicit return and implicit returns because you will see both in real world apps.

hope that helps :+1:

6 Likes

mod edit: code removed
it works

You can post solutions that invite discussion (like asking how the solution works, or asking about certain parts of the solution). But please don’t just post your solution for the sake of sharing it.
If you post a full passing solution to a challenge and have questions about it, please surround it with [spoiler] and [/spoiler] tags on the line above and below your solution code.