简体   繁体   English

变量定义问题Javascript

[英]Variable Defining Issue Javascript

I'm trying to randomize a string depending on how many players their are and assign each player their job/role. 我正在尝试根据他们是多少个球员来随机化一个字符串,并为每个球员分配他们的工作/角色。 In the Chrome Console I am getting the error, "Uncaught ReferranceError: al is not defined." 在Chrome控制台中,我收到错误消息:“未捕获的ReferranceError:未定义。” I dont get what the issue is, the variable is defined before using it(defined in first button, used in second). 我不明白问题是什么,变量是在使用它之前定义的(在第一个按钮中定义,在第二个中使用)。 I have an alert in the document that proves the variable is defined, and when pressing the button it says in the parameters al. 我在文档中有一个警报,证明已定义了变量,并且在按下按钮时会在参数al中指出。 HTML: 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: 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!");   
    }
}

You have two function, DJ and BlankDisplay (nevermind the use of caps) that both take arguments of al and rl, but second function doesn't make use of al, or rl. 您有两个函数DJ和BlankDisplay(不要使用大写字母)都使用al和rl的参数,但是第二个函数不使用al或rl。 Then in this code: <button id="DJ" onclick="DJ(al, rl) you pass in the variables al and rl into the function call for DJ, but unless you've defined the var al and rl as properties on the window/global scope, then those are undefined. 然后在此代码中: <button id="DJ" onclick="DJ(al, rl)您将变量al和rl传递到DJ的函数调用中,但是除非您已将var al和rl定义为窗口/全局范围,则未定义。

I'm guessing you're confused about the distinction between a variable and and an argument. 我猜您对变量和参数之间的区别感到困惑。 So I'd start there. 所以我将从这里开始。

Remove the al & rl from your DJ parameters. 从DJ参数中删除alrl They live in a global scope and should have access to al & rl WITHOUT the need of passing it via the DJ button's onclick . 他们生活在全球范围内,无需访问DJ按钮的onclick即可访问alrl

  • Update button DJ like so: <button id="DJ" onclick="DJ(); BlankDisplay();">Display Your Job!</button> . 像这样更新按钮DJ<button id="DJ" onclick="DJ(); BlankDisplay();">Display Your Job!</button>
  • Add al and rl before everything in your JS like so: var al, rl; 在JS中的所有内容之前添加alrl ,如下所示: var al, rl; .
  • Omit var in your Randomize. 在您的Randomize中忽略var So this: var rl = shuffle(positions); var al = positions.length; 因此: var rl = shuffle(positions); var al = positions.length; var rl = shuffle(positions); var al = positions.length; should become this: rl = shuffle(positions); al = positions.length; 应该变成这样: rl = shuffle(positions); al = positions.length; rl = shuffle(positions); al = positions.length; .
  • Change this line function DJ(al, rl){ to this function DJ(){ . 将此行function DJ(al, rl){更改为此function DJ(){
  • Change this line function BlankDisplay(al, rl){ to this function BlankDisplay(){ . 将此行function BlankDisplay(al, rl){更改为此function BlankDisplay(){

This should do. 这应该做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM