简体   繁体   English

全局变量不会改变功能

[英]Global variable won't change in function

To practice coding, I am making a virtual casino, with a roulette wheel. 为了练习编码,我正在制作一个带有轮盘的虚拟赌场。 For simplicity's sake, the wheel currently has only 4 sections, instead of the usual 37. I'm trying to have a "money" variable that adjusts every time the player spins the roulette wheel. 为了简单起见,方向盘目前只有4个部分,而不是通常的37个部分。我正在尝试使用一个“ money”变量,该变量在玩家每次旋转轮盘时都会进行调整。 IE if the player bets $10 on number 4 and loses, they will now have $190 instead of $200. IE,如果玩家在第4位下注10美元并输了,他们现在将获得190美元而不是200美元。 The problem is, the "money" variable does not seem to change, even though its a global variable. 问题是,即使是全局变量,“ money”变量似乎也没有改变。

Here's a piece of my code. 这是我的一部分代码。 What is the problem? 问题是什么?

var money = 200;

function spin(){
  var landing = Math.floor(Math.random() * 4) + 1;   

  var nsvalue = parseInt(document.regular_roulette.num_select.value);

  var betvalue = parseInt(document.regular_roulette.bet.value);

  if (landing === nsvalue) {
    alert("You win $" + betvalue * 3);

    money = money + (betvalue * 3);
  } else {
    alert("You lose $" + betvalue);

    money = money - betvalue;
  }
}

document.write("You currently have $" + money);

Normally you might put this in some input fields, check those values (check for not a number) and output to some element on the page. 通常,您可以将其放在某些输入字段中,检查那些值(检查是否为数字),然后输出到页面上的某些元素。 Here I use a spin button click for the spin action. 在这里,我使用旋转按钮单击来进行旋转操作。 Example. 例。

 var money = 200; function spin() { var landing = Math.floor(Math.random() * 4) + 1; var nsvalue = parseInt(document.getElementById("num_select").value); var betvalue = parseInt(document.getElementById("bet").value); if(!isNaN(nsvalue)&& !isNaN(betvalue)) if (landing === nsvalue) { document.getElementById("action").innerText =("You win $" + betvalue * 3); money = money + (betvalue * 3); } else { document.getElementById("action").innerText =("You lose $" + betvalue); money = money - betvalue; } //something here to show spin value perhaps? } document.getElementById("spinme").onclick = function(event) { spin(); document.getElementById("results").innerText = ("You currently have $" + money); } 
 <div id="results"> empty </div> <div id="regular_roulette"> <label>Choice <input id="num_select"> </label> <label>Bet <input id="bet"> </label> </div> <button id="spinme"> Spin! </button> <div id="action"> </div> 

Instead of using a global variable. 而不是使用全局变量。 you can try to pass it to the function and return it and reassign it. 您可以尝试将其传递给函数,然后返回并重新分配。

var money = 200;

        function spin(m){



                    var landing = Math.floor(Math.random() * 4) + 1;   

                    var nsvalue = parseInt(document.regular_roulette.num_select.value);

                    var betvalue = parseInt(document.regular_roulette.bet.value);


                    if (landing === nsvalue)
                    {
                        alert("You win $" + betvalue * 3);

                        m = m + (betvalue * 3);


                    }

                    else
                    {
                        alert("You lose $" + betvalue);

                        m = m - betvalue;


                    }
                    return m;
        }

    money = spin(m);

    document.write("You currently have $" + money);

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

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