变量定义问题 Javascript
Variable Defining Issue Javascript
我正在尝试根据玩家数量随机化一个字符串,并为每个玩家分配 job/role。在 Chrome 控制台中我收到错误消息,
"Uncaught ReferranceError: al is not defined." 我不明白问题出在哪里,变量是在使用它之前定义的(在第一个按钮中定义,在第二个按钮中使用)。
我在证明变量已定义的文档中有一个警报,当按下按钮时,它在参数 al 中显示。
HTML:
<input maxlength="1" id="input"> <button id="gp" onclick="gp()">Start!</button>
<br/>
<br/>
<button id="DJ" onclick="DJ(al, rl);BlankDisplay(al, rl)">Display Your Job!</button>
<br/>
<span id="DS">1) Input Amount Of Players. 2)Click 'Display Your Job!'</span>
Javascript:
function gp(){
players = document.getElementById("input").value;
if(isNaN(players)){
alert(players.toUpperCase() + "'s Players? Please Fix");
return;
}
if(players === " "){
alert("Please Define How Many People Are Playing!");
return;
}
if(players === ""){
alert("Please Define How Many People Are Playing!");
return;
}
if(players < 4){
alert("Sorry, You Need Atleast 4 Players To Play!");
return;
}
SA(players)
}
function SA(players){
var positions = ["Murderer", "Judge", "Innocent", "Innocent"]; //Pre-set positions
if(players == 5){
positions.push("Co-Judge");
}else if(players == 6){
positions.push("Innocent", "Co-Judge");
}else if(players == 7){
positions.push("Murderer-2!", "Innocent", "Co-Judge");
}
Randomize(players, positions)
}
function shuffle(o){
for(var j, x, i = o.length; i; j = Math.floor(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
return o;
}
function Randomize(players, positions){
var rl = shuffle(positions);
var al = positions.length;
confirm("You Have: " + al + " Players, Correct?");
alert(al + ". " + rl);
}
function DJ(al, rl){
var counter = 0;
var bd = 0;
for(var c = 0; c < al + 1; c++){
if(counter == 0){
document.getElementById("DS").innerHTML(rl[c]);
document.getElementById("BJ").innerHTML("Click To Clear!");
bd = 1;
}
}
}
function BlankDisplay(al, rl){
if(bd == 1){
document.getElementById("Click The Button Above To See Your Job!");
}
}
您有两个函数,DJ 和 BlankDisplay(不要介意使用大写字母),它们都接受 al 和 rl 的参数,但第二个函数不使用 al 或 rl。然后在这段代码中: <button id="DJ" onclick="DJ(al, rl)
将变量 al 和 rl 传递到 DJ 的函数调用中,但是除非您已将变量 al 和 rl 定义为 window/global 范围内的属性,否则它们是未定义。
我猜您对变量和参数之间的区别感到困惑。所以我会从这里开始。
从您的 DJ 参数中删除 al
和 rl
。他们生活在全球范围内,应该可以访问 al
& rl
而无需通过 DJ
按钮的 onclick
.
传递它
- 更新按钮
DJ
像这样:<button id="DJ" onclick="DJ(); BlankDisplay();">Display Your Job!</button>
.
- 在您的 JS 中的所有内容之前添加
al
和 rl
,如下所示:var al, rl;
.
- 在随机化中省略
var
。所以这个:var rl = shuffle(positions); var al = positions.length;
应该变成这个:rl = shuffle(positions); al = positions.length;
.
- 将此行
function DJ(al, rl){
更改为 function DJ(){
。
- 将此行
function BlankDisplay(al, rl){
更改为 function BlankDisplay(){
。
这应该可以。
我正在尝试根据玩家数量随机化一个字符串,并为每个玩家分配 job/role。在 Chrome 控制台中我收到错误消息, "Uncaught ReferranceError: al is not defined." 我不明白问题出在哪里,变量是在使用它之前定义的(在第一个按钮中定义,在第二个按钮中使用)。 我在证明变量已定义的文档中有一个警报,当按下按钮时,它在参数 al 中显示。 HTML:
<input maxlength="1" id="input"> <button id="gp" onclick="gp()">Start!</button>
<br/>
<br/>
<button id="DJ" onclick="DJ(al, rl);BlankDisplay(al, rl)">Display Your Job!</button>
<br/>
<span id="DS">1) Input Amount Of Players. 2)Click 'Display Your Job!'</span>
Javascript:
function gp(){
players = document.getElementById("input").value;
if(isNaN(players)){
alert(players.toUpperCase() + "'s Players? Please Fix");
return;
}
if(players === " "){
alert("Please Define How Many People Are Playing!");
return;
}
if(players === ""){
alert("Please Define How Many People Are Playing!");
return;
}
if(players < 4){
alert("Sorry, You Need Atleast 4 Players To Play!");
return;
}
SA(players)
}
function SA(players){
var positions = ["Murderer", "Judge", "Innocent", "Innocent"]; //Pre-set positions
if(players == 5){
positions.push("Co-Judge");
}else if(players == 6){
positions.push("Innocent", "Co-Judge");
}else if(players == 7){
positions.push("Murderer-2!", "Innocent", "Co-Judge");
}
Randomize(players, positions)
}
function shuffle(o){
for(var j, x, i = o.length; i; j = Math.floor(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
return o;
}
function Randomize(players, positions){
var rl = shuffle(positions);
var al = positions.length;
confirm("You Have: " + al + " Players, Correct?");
alert(al + ". " + rl);
}
function DJ(al, rl){
var counter = 0;
var bd = 0;
for(var c = 0; c < al + 1; c++){
if(counter == 0){
document.getElementById("DS").innerHTML(rl[c]);
document.getElementById("BJ").innerHTML("Click To Clear!");
bd = 1;
}
}
}
function BlankDisplay(al, rl){
if(bd == 1){
document.getElementById("Click The Button Above To See Your Job!");
}
}
您有两个函数,DJ 和 BlankDisplay(不要介意使用大写字母),它们都接受 al 和 rl 的参数,但第二个函数不使用 al 或 rl。然后在这段代码中: <button id="DJ" onclick="DJ(al, rl)
将变量 al 和 rl 传递到 DJ 的函数调用中,但是除非您已将变量 al 和 rl 定义为 window/global 范围内的属性,否则它们是未定义。
我猜您对变量和参数之间的区别感到困惑。所以我会从这里开始。
从您的 DJ 参数中删除 al
和 rl
。他们生活在全球范围内,应该可以访问 al
& rl
而无需通过 DJ
按钮的 onclick
.
- 更新按钮
DJ
像这样:<button id="DJ" onclick="DJ(); BlankDisplay();">Display Your Job!</button>
. - 在您的 JS 中的所有内容之前添加
al
和rl
,如下所示:var al, rl;
. - 在随机化中省略
var
。所以这个:var rl = shuffle(positions); var al = positions.length;
应该变成这个:rl = shuffle(positions); al = positions.length;
. - 将此行
function DJ(al, rl){
更改为function DJ(){
。 - 将此行
function BlankDisplay(al, rl){
更改为function BlankDisplay(){
。
这应该可以。