It’s close. The isCover function just need to return that comparison, and with covers.forEach, I was tired at the time so my apologies, but covers needs to be allCovers instead.
As testing, to check that things are right, when console.log the players array you should see the following:
Yes that look good. There’s just the return after the index variable to do now. The players array has an object at that index. That object has a wrapper property. We want to return the wrapper that is at the index of the players array.
It is the end of the getWrapper function that needs a bit more. I think that we are going to have to split things up a bit more for you to cope with it.
At the end of the getWrapper we need a wrapper variable to be defined, called wrapper. That wrapper is retrieved from the players object at the index location. The item at that index location is an object with both cover and wrapper properties. You need the wrapper property from that item.
To possibly explain a bit better, what the getWrapper function in big-picture terms does is to look through the list of players, searching for the cover. When we find the cover we return the matching wrapper.
You now have an index variable in your getWrapper function. That index is a number, which represents the location of the wrapper that we need.
You need to use that index to access an item in the players array. That item has a wrapper property that needs to be returned out of the getWrapper function.
That wrapper is retrieved from the players object at the index location. The item at that index location is an object with both cover and wrapper properties. You need the wrapper property from that item.
I mis-spoke earlier, for the players variable isn’t an object, it’s an array. I’m sorry for any confusion there.
There is a players variable which is an array, containing information about the covers and wrapper.
Lets start with the wrapper variable, and assign to it the players variable.
That gives you the whole array of items that is in the players variable.
We need less than the full array. Only one item from that array is wanted, so on the same line and after that players variable, use index in square brackets get an item from the array at that index.
That gives you an object with a cover property and a wrapper property.
We need less than the full object. We only want the wrapper property from that object, so after the square brackets add .wrapper
Then return that wrapper variable from the function.
I did say that it needs to be done at the end of the getWrapper function. That means still inside of the function at the end. Not after the function below it.
Which one to use can different based on several different factors, such as function size and overall code complexity. Here, my preference is for neither of them. It’s to use arrow-notation instead when there’s only one line in the function.
// arrow function
function getWrapper(cover) {
function isCover = (player) => player.cover === cover;
const index = players.findIndex(isCover);
const wrapper = players[index].wrapper;
return wrapper;
}
I would also rename isCover to hasCover. It’s a small difference, but provides a more accurate understanding about things.
// arrow function, hasCover instead of isCover
function getWrapper(cover) {
const hasCover = (player) => player.cover === cover;
const index = players.findIndex(hasCover);
const wrapper = players[index].wrapper;
return wrapper;
}
[edited to correct an issue]
However, when the code is this simple, I would go with using an inline arrow function, so that the simplicity of the function tells you what is going on.
// inline arrow function
function getWrapper(cover) {
const index = players.findIndex(
(player) => player.cover === cover
);
const wrapper = players[index].wrapper;
return wrapper.
}
Also, I would directly return the wrapper instead of assigning it to a wrapper variable.
// inline arrow function, direct return
function getWrapper(cover) {
const index = players.findIndex(
(player) => player.cover === cover
);
return players[index].wrapper;
}