我想让一个代码这样工作:
- 获取球队数量(y)和球员数量(x)。
- 获取x个名字和x个排名。
- 根据玩家排名创建平衡团队。每支队伍应该有x/y名队员。
- 分别打印每支队伍。
我看不出我的问题在哪里,为什么我的代码不工作。希望你们能帮忙。
这是我的代码:
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>
在这里查看我的解决方案。应该没问题。你错误地将循环中的逻辑转换为向团队中添加玩家。此外,使用Array.prototype.push
比在使用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;
}