JavaScript function to create 2 soccer teams as even as possible, from a list of players
First I would make each team an object with a score and players rather than an object for scores and one for players:

var teams = [ { name:'A'; score: 0, players:[] },
              { name:'B', score: 0, players:[] };


Once you have sorted the players by score it might be simpler to pick them off the list two at a time and assign the one with the higher score to the lower scoring team:

// Note: slice is probably better than Object.assign 
let input = this.players.slice();
assert(input.length % 2 == 0, 'Need an equal number of players');

while (input.length &gt; 0) {
  let [strongerTeam,   weakerTeam]   = teams;
  let [strongerPlayer, weakerPlayer] = input.slice(0,2);
  addPlayer(strongerTeam, weakerPlayer);
  addPlayer(weakerTeam,   strongerPlayer);
  sortTeams(teams); 
}

function addPlayer(team, player) {
  team.players.push(player);
  team.score += player.score;
}

// Sort highest scoring team first
function sortTeams(teams) {
  teams.sort(function(a, b) { return b.score - a.score;});      
}

return teams;


One advantage is this always returns the more powerful team first.



This algorithm is not ideal in that it doesn't always choose the most balanced team, for example given players with scores of 8, 4, 3, 3, 1 and 1, Team A will have players 8, 3 and 1 (score of 12) while team b will have players with scores of 4, 3 and 1 (totaling 8). It might be more reasonable to balance them as Team A: 8, 1, 1 and Team B 4, 3, 3 so that both teams have a score of 10. The logic for that might be:

while (input.length &gt; 0) {
  let [strongerTeam,   weakerTeam]   = teams;
  let nextPlayer = input.slice(0,1)[0];
  addPlayer(weakerTeam,   nextPlayer);
  if (strongerTeam.length - weakerTeam.length == input.length) {
    addPlayers(strongerTeam, input);
    input = [];
  }

  sortTeams(teams); 
}

function addPlayers(team, players) {
  for (let player of players) {
    addPlayer(team, player);
  }
}