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

Tell us what’s happening:

My filtered codes are failing always, whereas the UI is working correctly. I think there is a bug in the program itself. Could you check it out?

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: 1986,
    headCoach: "Carlos Bilardo",
    players: [
        {
            name: "Sergio Almirón",
            position: "forward",
            isCaptain: false
        },
        {
            name: "Sergio Batista",
            position: "midfielder",
            isCaptain: false
        },
        {
            name: "Ricardo Bochini",
            position: "midfielder",
            isCaptain: false
        },
        {
            name: "Claudio Borghi",
            position: "midfielder",
            isCaptain: false
        },
        {
            name: "José Luis Brown",
            position: "defender",
            isCaptain: false
        },
        {
            name: "Daniel Passarella",
            position: "defender",
            isCaptain: false
        },
        {
            name: "Jorge Burruchaga",
            position: "forward",
            isCaptain: false
        },
        {
            name: "Néstor Clausen",
            position: "defender",
            isCaptain: false
        },
        {
            name: "José Luis Cuciuffo",
            position: "defender",
            isCaptain: false
        },
        {
            name: "Diego Maradona",
            position: "midfielder",
            isCaptain: true
        },
        {
            name: "Jorge Valdano",
            position: "forward",
            isCaptain: false
        },
        {
            name: "Héctor Enrique",
            position: "midfielder",
            isCaptain: false
        },
        {
            name: "Oscar Garré",
            position: "defender",
            isCaptain: false
        },
        {
            name: "Ricardo Giusti",
            position: "midfielder",
            isCaptain: false
        },
        {
            name: "Luis Islas",
            position: "goalkeeper",
            isCaptain: false
        },
        {
            name: "Julio Olarticoechea",
            position: "defender",
            isCaptain: false
        },
        {
            name: "Pedro Pasculli",
            position: "forward",
            isCaptain: false
        },
        {
            name: "Nery Pumpido",
            position: "goalkeeper",
            isCaptain: false
        },
        {
            name: "Oscar Ruggeri",
            position: "defender",
            isCaptain: false
        },
        {
            name: "Carlos Tapia",
            position: "midfielder",
            isCaptain: false
        },
        {
            name: "Marcelo Trobbiani",
            position: "midfielder",
            isCaptain: false
        },
        {
            name: "Héctor Zelada",
            position: "goalkeeper",
            isCaptain: false
        }
    ]
}

// 2) Display the team, year, and head coach in their respective elements
document.getElementById("team").textContent = footballTeam.team;
document.getElementById("year").textContent = footballTeam.year;
document.getElementById("head-coach").textContent = footballTeam.headCoach;

// 3) Build player cards inside player-cards
const playerCardsContainer = document.getElementById("player-cards");

footballTeam.players.forEach(player => {
    const cardDiv = document.createElement("div");
    cardDiv.classList.add("player-card");

    // (a) Name 
    const nameHeading = document.createElement("h2");
    nameHeading.textContent = player.isCaptain
        ? `(Captain) ${player.name}`
        : player.name;

    // (b) Position 
    const positionPara = document.createElement("p");

    positionPara.textContent = `Position: ${player.position}`;

    // Append to the card
    cardDiv.appendChild(nameHeading);
    cardDiv.appendChild(positionPara);

    // Add the card to player-cards
    playerCardsContainer.appendChild(cardDiv);
});

// 4) Filtering logic 

const filterEl = document.getElementById("players");

filterEl.addEventListener("change", () => {
    const selectedValue = filterEl.value;
    const allCards = document.querySelectorAll(".player-card");

    allCards.forEach(card => {
        const positionText = card.querySelector("p").textContent;
        const cardPosition = positionText.replace("Position: ", "").trim();
        console.log(cardPosition);
        
        if (selectedValue === "all") {
            card.style.display = "block";
        } else if (selectedValue === "forward" && cardPosition === "forward") {
            card.style.display = "block";
        } else if (selectedValue === "midfielder" && cardPosition === "midfielder") {
            card.style.display = "block";
        } else if (selectedValue === "defender" && cardPosition === "defender") {
            card.style.display = "block";
        } else if (selectedValue === "goalkeeper" && cardPosition === "goalkeeper") {
            card.style.display = "block";
        } else {
            card.style.display = "none";
        }
    });
});

Your browser information:

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

Challenge Information:

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

you need to not have the cards there instead of hiding them

I can’t understand. Can you tell me in brief?

you are hiding the card, but it is still present in the html, that means that the tests still see them, you need to remove them completel;y