skip to Main Content

I am building an app for selecting 11 players from Team A and Team B

I am using below logic to select players from Team A and Team B:-

{
  name: 'Logic 7',
  teamAIndices: [0, 3, 4, 9, 10],
  teamBIndices: [0, 1, 4, 5, 7, 10],
  captainIndex: [10], // 1st player from Team A
  viceCaptainIndex: [6], // 3rd player from Team A
},

But whenever i am selecting teamBIndices(TeamB) last player as 10th (means 11th player) and captainIndex (Means captain of the team) as 10th player (means 11th player of the created team), it is giving me only 10 player. After logging it is found that teamBIndices(TeamB) last player as 10th (means 11th player) is not being selected, but i don’t know why.

Apart from this if i am using below logic, it is creating complete 11 players team:-

{
  name: 'Logic 6',
  teamAIndices: [1, 4, 5, 6, 8, 10],
  teamBIndices: [0, 3, 4, 6, 10],
  captainIndex: [6], // 1st player from Team A
  viceCaptainIndex: [8], // 3rd player from Team A
},

I need to select 11 players for a team. I am working on javascript.

I am creating team like this:-

function createTeam(teamA, teamB, config) {
  const selectedPlayers = [
    ...config.teamAIndices.map((index) => teamA[index]),
    ...config.teamBIndices.map((index) => teamB[index]),
  ];

  const captainIndex = config.captainIndex;
  const viceCaptainIndex = config.viceCaptainIndex;

  // Assign Captain and Vice Captain without adding line breaks
  selectedPlayers[captainIndex] += ' (C)';
  selectedPlayers[viceCaptainIndex] += ' (VC)';
  return selectedPlayers; // Return the array of players
}

2

Answers


  1. Chosen as BEST ANSWER

    Actually the code is ok, the problem with the function i use to remove (C) or (VC) that is placed under the player name in a separate line during creation of team. Below is the code which is deleting the line which contains (C) and (VC) so, if am choosing captainindex(10) it was making Captain to 11th player which was deleted by the function below and moving that (C) to the 10th player:-

    const modifiedFormattedTeam = formattedTeam.join('n')
          .replace(/r?n?.*(C).*$/gm, ' (C)')
          .replace(/r?n?.*(VC).*$/gm, ' (VC)');
    

  2. You should call:

    selectedPlayers[captainIndex[0]]
    

    and

    selectedPlayers[viceCaptainIndex[0]]
    

    or reset your config to:

    captainIndex: 6, // 1st player from Team A
    viceCaptainIndex: 8, // 3rd player from Team A
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search