? Given a list of players with their respective scores (how well does a player play soccer, from 1 to 5 points), return two teams that are as even as possible.I've created this code, which is a first version, but I really would like to hear about ideas of how can I improve it.exports.TeamCreator = (function () {    this.players = [];    function setPlayers(players){        this.players = Object.assign([], players);    }    function createTeams() {        var input = this.players;        var teams = [],            i = 0,            scores = [],            max = input.length;        scores[A]=0; scores[B]=0;        teams[A]=[]; teams[B]=[];        input.sort(function(a, b){ return b.score - a.score;});        var nextPlayer;        var teamForNextPlayer;        for (i = 0; i < max; i++){            next();            teams[teamForNextPlayer].push(nextPlayer);            scores[teamForNextPlayer] += nextPlayer.score;        }        function getBest() {            nextPlayer = input.splice(0, 1)[0]; //Best in list        }        function getWorst() {            nextPlayer =  input.splice(input.length - 1, 1)[0]; //Worst in list        }        function next(){            var counterA = teams[A],                counterB = teams[B],                scoreA = scores[A],                scoreB = scores[B];            if(counterA > counterB) { //B has less players than A                teamForNextPlayer = B;                if(scoreB >= scoreA){                    getWorst();                }else{                    getBest();                }            }            else if(counterA < counterB) { //A has less players than B                teamForNextPlayer = A;                if(scoreA >= scoreB){                    getWorst();                }else{                    getBest();                }            }            else //Same amount of players            {                getBest();                teamForNextPlayer = (scoreA >= scoreB) ? B : A;            }        }        return {            teamA: teams[A],            teamB: teams[B],            diff: Math.abs(scores[A] - scores[B]),            scoreA: scores[A],            scoreB: scores[B]        };    }    return {        setPlayers : setPlayers,        createTeams: createTeams    }}());If it's easier, you can pull the code from here.
JavaScript function to create 2 soccer teams as even as possible, from a list of players
javascript;algorithm