我正在设置一个篮球项目,只需按一下按钮即可从头到尾运行。但是错误控制台说我的Teamarray没有定义。我的代码出了什么问题?
function teamArray(list)
//Assumes: all basketball teams
//Returns: a random team from the list
{
var teamArray, randomIndex;
teamArray = ['Sales', 'HR', 'Accounting', 'Marketing', 'Engineering', 'Manufacturing', 'IT', 'Legal'];
randomIndex = RandomInt(0, list.length-1);
index=0;
while (index < teamArray.length) {
teamArray[index] = randomIndex;
index = index * 1;
}
return teamArray;
document.getElementById('team1').value = teamArray[0];
document.getElementById('team2').value = teamArray[1];
document.getElementById('team3').value = teamArray[2];
document.getElementById('team4').value = teamArray[3];
document.getElementById('team5').value = teamArray[4];
document.getElementById('team6').value = teamArray[5];
document.getElementById('team7').value = teamArray[6];
document.getElementById('team8').value = teamArray[7];
}
编辑:
<input type="button" value="Start Tournament" onclick="teamArray(list); AddBonus(); TotalScore(); Winner(); ">
答案 0 :(得分:0)
在您的代码中:
> randomIndex = RandomInt(0, list.length-1);
RandomeInt
返回什么?它是0和list.length-1之间的随机数吗?
> index=0;
标识符index
未声明,此时它将成为全局变量。
> while (index < teamArray.length) {
> teamArray[index] = randomIndex;
> index = index * 1;
将数字乘以1只返回数字,这应该会产生无限循环,也许你的意思是:
index += 1;
或
++index;
。
} 返回teamArray;
执行返回后没有代码。我不确定你要做什么,你期待teamArray是什么?一系列数字?来自原始团队的成员是什么?您好像用数字替换了声明的成员。