Build a Set of Football Team Cards - Build a Set of Football Team Cards

Tell us what’s happening:

Hi y’all, the project seems to be working as expected but steps 12-15 are refusing to pass no matter what I try. Can anyone see what might be the problem here?

Your code so far

<!-- file: index.html -->
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>
      Build a Set of Football Team Cards
    </title>
    <link rel="stylesheet" href="styles.css" />
  </head>
  <body>
    <h1 class="title">Team stats</h1>
    <main>
      <div class="team-stats">
        <p>Team: <span id="team"></span></p>
        <p>Year: <span id="year"></span></p>
        <p>Head coach: <span id="head-coach"></span></p>
      </div>
      <label class="options-label" for="players">Filter Teammates:</label>
      <select name="players" id="players">
        <option value="all">All Players</option>
        <option value="forward">Position Forward</option>
        <option value="midfielder">Position Midfielder</option>
        <option value="defender">Position Defender</option>
        <option value="goalkeeper">Position Goalkeeper</option>
      </select>
      <div class="cards" id="player-cards"></div>
    </main>
    <footer>&copy; freeCodeCamp</footer>
    <script src="./script.js"></script>
  </body>
</html>

/* file: styles.css */
*,
*::before,
*::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

:root {
  --dark-grey: #0a0a23;
  --light-grey: #f5f6f7;
  --white: #ffffff;
  --black: #000;
}

body {
  background-color: var(--dark-grey);
  text-align: center;
  padding: 10px;
}

.title,
.options-label,
.team-stats,
footer {
  color: var(--white);
}

.title {
  margin: 1.3rem 0;
}

.team-stats {
  display: flex;
  justify-content: space-around;
  flex-wrap: wrap;
  font-size: 1.3rem;
  margin: 1.2rem 0;
}

.options-label {
  font-size: 1.2rem;
}

.cards {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  align-items: center;
}

.player-card {
  background-color: var(--light-grey);
  padding: 1.3rem;
  margin: 1.2rem;
  width: 300px;
  border-radius: 15px;
}

@media (max-width: 768px) {
  .team-stats {
    flex-direction: column;
  }
}

/* file: script.js */
const footballTeam = {
  team: 'Argentina',
  year: 2022,
  headCoach: 'Lionel Sebastián Scaloni',
  players: [
    {
      name: 'G. Rulli',
      position: 'goalkeeper',
      isCaptain: false
    },
    {
      name: 'E. Martínez',
      position: 'goalkeeper',
      isCaptain: false
    },
    {
      name: 'F. Armani',
      position: 'goalkeeper',
      isCaptain: false
    },
    {
      name: 'G. Montiel',
      position: 'defender',
      isCaptain: false
    },
    {
      name: 'C. Romero',
      position: 'defender',
      isCaptain: false
    },
    {
      name: 'N. Tagliafico',
      position: 'defender',
      isCaptain: false
    },
    {
      name: 'J. Foyth',
      position: 'defender',
      isCaptain: false
    },
    {
      name: 'G. Pezzella',
      position: 'defender',
      isCaptain: false
    },
    {
      name: 'Lisandro Martínez',
      position: 'defender',
      isCaptain: false
    },
    {
      name: 'M. Acuña',
      position: 'defender',
      isCaptain: false
    },
    {
      name: 'N. Molina',
      position: 'defender',
      isCaptain: false
    },
    {
      name: 'N. Otamendi',
      position: 'defender',
      isCaptain: false
    },
    {
      name: 'E. Fernández',
      position: 'midfielder',
      isCaptain: false
    },
    {
      name: 'R. De Paul',
      position: 'midfielder',
      isCaptain: false
    },
    {
      name: 'T. Almada',
      position: 'midfielder',
      isCaptain: false
    },
    {
      name: 'E. Palacios',
      position: 'midfielder',
      isCaptain: false
    },
    {
      name: 'G. Rodríguez',
      position: 'midfielder',
      isCaptain: false
    },
    {
      name: 'Á. Di María',
      position: 'midfielder',
      isCaptain: false
    },
    {
      name: 'P. Gómez',
      position: 'midfielder',
      isCaptain: false
    },
    {
      name: 'A. Mac Allister',
      position: 'midfielder',
      isCaptain: false
    },
    {
      name: 'L. Paredes',
      position: 'midfielder',
      isCaptain: false
    },
    {
      name: 'Á. Correa',
      position: 'forward',
      isCaptain: false
    },
    {
      name: 'Lautaro Martínez',
      position: 'forward',
      isCaptain: false
    },
    {
      name: 'P. Dybala',
      position: 'forward',
      isCaptain: false
    },
    {
      name: 'J. Álvarez',
      position: 'forward',
      isCaptain: false
    },
    {
      name: 'L. Messi',
      position: 'forward',
      isCaptain: true
    },
  ]
};

const teamSpan = document.getElementById('team');
const yearSpan = document.getElementById('year');
const headCoachSpan = document.getElementById('head-coach');

teamSpan.innerText = footballTeam.team;
yearSpan.innerText = footballTeam.year;
headCoachSpan.innerText = footballTeam.headCoach;

const playerCardsDiv = document.getElementById('player-cards');

footballTeam.players.forEach((player) => {
  const playerDiv = document.createElement('div');
  playerDiv.setAttribute('class', 'player-card');
  playerCardsDiv.appendChild(playerDiv);
  const playerH2 = document.createElement('h2');
  playerDiv.appendChild(playerH2);
  const playerP = document.createElement('p');
  playerDiv.appendChild(playerP);
  playerP.innerText = `Position: ${player.position}`;
  if (player.isCaptain) {
    playerH2.innerText = `(Captain) ${player.name}`;
  } else {
    playerH2.innerText = `${player.name}`;
  };
});

const positionFilter = document.getElementById('players');

function filterPlayers() {
  const playerCards = document.querySelectorAll('.player-card');
  const selectedPosition = positionFilter.value;
  playerCards.forEach((playerCard) => {
    if (selectedPosition === 'all') {
      playerCard.style.display = 'inline-block';
    } else if (playerCard.innerText.indexOf(selectedPosition) !== -1) {
      playerCard.style.display = 'inline-block';
    } else {
      playerCard.style.display = 'none';
    };
  });
};

positionFilter.addEventListener('input', filterPlayers);

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36

Challenge Information:

Build a Set of Football Team Cards - Build a Set of Football Team Cards

Can you talk about what tests 12-15 are and what you’ve tried to check if your code meets those requirements?

    1. When the option Position Forward is selected, only forward players should be shown within #player-cards.
    1. When the option Position Midfielder is selected, only midfielder players should be shown within #player-cards.
    1. When the option Position Defender is selected, only defender players should be shown within #player-cards.
    1. When the option Position Goalkeeper is selected, only goalkeeper players should be shown.
      My code meets these requirements in practice but not in the tests, for whatever reason. I’ve tried multiple methods, all of which have displayed the correct cards at the correct times, but none of which have passed the tests.

What does this mean? Can you say more about what specifically you have used to verify this?

I suspect this logic is the problem - you’re hiding cards instead of removing them.

Yes, when I change the dropdown the correct cards are showing up (i.e. when I select a position only that position’s cards show up)

I’ll try this, thank you!

Removing the cards is not working as intended; the cards do not regenerate and I’m eventually left with none of them. Shouldn’t hiding them be sufficient, if that’s what the lab is asking for?

No, hiding is not sufficient.

You should not destroy any of the contents of your array of players. The HTML and the JS array are separate.

I was using the DOM .remove() method. Currently trying to remove from the JS array instead

You SHOULD NOT destroy any of the contents of your array of players.

Heard. I’m having trouble understanding the solution you’re suggesting.

Here you assumed that you always want every single card to always be present on the HTML. Why not remove this assumption and only put cards in the HTML when requested?

Yes, I was considering this but encountered the problem that under this method, the cards are not shown on the page until the dropdown is clicked, as the functions then have to be combined and only called under positionFilter.addEventListener()

I don’t see why this is a problem?

I do not know what this means.