4

I'm trying to make a code that works like this:

  1. Get number of teams (y) and number of players(x).
  2. Get x names and x ranks.
  3. Create balance teams based on the player ranks. Each team should have x/y players.
  4. Print each team separately.

I can't see where is my problem and why my code doesn't work. Hope you guys could help.

This is my code:

function step1() {
	var teams = document.getElementById("teams").value;
	var players = document.getElementById("players").value;
	var main = document.getElementById("main");
	main.innerHTML="";
	for(var i=1;i<=players;i++){ 
		main.innerHTML += "<input class='names' placeholder='Player "+i+"' type='text' style='width:100px'/>   "+ 
		"<input class='ranks' placeholder='Rank' type='text' style='width:40px'/><br/>";
	}
	main.innerHTML+="<br/><button onclick='buildTeams("+players+","+teams+")'>Build</button>";
}
function buildTeams(playersInt,teamsInt) {
	var error=0;
	var names = new Array(playersInt);	
	var ranks = new Array(playersInt);
	var nameInp = document.getElementsByClassName("names");	
	var rankInp = document.getElementsByClassName("ranks");
	for(var i=0;i<playersInt;i++) {
		names[i] = nameInp[i].value;	
	}
	for(var i=0;i<playersInt;i++) {
		ranks[i] = rankInp[i].value;
	}
	var teams = new Array(teamsInt);
	for(var i=0;i<teamsInt;i++) {
		teams[i]=new Array(playersInt/teamsInt);
	}
	for(var i=0;i<(playersInt/teamsInt);i++) {
		for(var j=0;j<teamsInt;j++) {
			teams[i][j]=names[checkMvp(ranks)];
			ranks[checkMvp(ranks)]=-1;
		}
	}
	for(var i=0;i<teamsInt;i++) {
		alert(teams[i]);
	}

}
function checkMvp(ranks) {
	var high= ranks[0];
	var bpi=0;
	for(var i=1;i<ranks.length;i++) {
		if(ranks[i]>high) {
			bpi=i;
		}
	}
	return bpi;
}
<h2>Power Balance</h2>
<div id="main">
Number of teams:
<input type="text" id="teams" style="width:30px"/> 
<br/><br/>
Number of players:
<input type="text" id="players" style="width:30px"/> 
<br/><br/>
<p id="error"></p>
<input type="button" onclick="step1()" value="Next"/>
</div>

4
  • teams[i]=new Array(playersInt/teamsInt); Uncaught RangeError: Invalid array length Commented May 31, 2015 at 11:39
  • @Jonathan How is this possible? The length should be fine. What should i write instead? Commented May 31, 2015 at 11:47
  • @rondeitch try 2 teams with 1 player. Commented May 31, 2015 at 12:16
  • @cychoi Ok, I forgot to check for validation. But after i added validation, what i need to do in order to make this code work? Commented May 31, 2015 at 13:04

1 Answer 1

1

Check out my solution here. It should be working fine. You mistakenly swapped the logic in the loop for adding players to teams. Also, it's a good habit to use Array.prototype.push than to create new element using a loop function after creating the array with new Array(length).

function step1() {
    var teams = document.getElementById("teams").value;
    var players = document.getElementById("players").value;
    var main = document.getElementById("main");
    main.innerHTML="";
    for(var i=1;i<=players;i++){ 
        main.innerHTML += "<input class='names' placeholder='Player "+i+"' type='text' style='width:100px'/>   "+ 
        "<input class='ranks' placeholder='Rank' type='text' style='width:40px'/><br/>";
    }
    main.innerHTML+="<br/><button onclick='buildTeams("+players+","+teams+")'>Build</button>";
}
function buildTeams(playersInt,teamsInt) {
    var error=0;
    var names = []; 
    var ranks = [];
    var nameInp = document.getElementsByClassName("names"); 
    var rankInp = document.getElementsByClassName("ranks");
    for(var i=0;i<playersInt;i++) {
        names.push(nameInp[i].value);   
    }
    for(var e=0;e<playersInt;e++) {
        ranks.push(rankInp[e].value);
    }
    var teams = [];
    for(var x=0;x<teamsInt;x++) {
        teams.push([]);
    }
    for(var a=0;a<teamsInt;a++) {
        for(var j=0;j<(playersInt/teamsInt);j++) {
            console.log(teams[a]);
            teams[a].push(names[checkMvp(ranks)]);
            ranks[checkMvp(ranks)]=-1;
        }
    }
    alert(teams);
    for(var w=0;w<teamsInt;w++) {
        alert(teams[w]);
    }

}
function checkMvp(ranks) {
    var high= ranks[0];
    var bpi=0;
    for(var i=1;i<ranks.length;i++) {
        if(ranks[i]>high) {
            bpi=i;
        }
    }
    return bpi;
}
Sign up to request clarification or add additional context in comments.

6 Comments

You should include relevant code in your answer, that way if the link goes dead this answer is still valid.
@Bwaxxlo Thank you very much! Your answer is very helpful. Although, if we sum up each group rank every time we can see huge differences between the ranks - so it turns out not balanced teams. I tried for hours to write a code that balances the teams. Do you have any ideas?
@rondeitch creating a balanced team is a matter of algorithm design and ranking relativity. Are they self-described ranks or do you also design your own ranking algorithm. If the ranking is pre-determined (i.e: your decision), then it's trivial to design one. Make sure both sides are equidistant from the mean (at least as close as possible). Basically make sure the difference standard deviation between the two groups is minimized. If the rankings are self-prescribed, then it's a whole another data mining problem in your hands.
@Bwaxxlo Each team rank is a sum of all the player ranks. The players rank is between 1-10. I checked and the present code gives teams with a gap of up to 10 points and I want it to transfer players from team to team in a way that balances the ranks of the teams.
@Bwaxxlo Do you have any idea how to do it with the code you wrote for me? Thank you for your help!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.