【问题标题】:Why can't I change variables?为什么我不能更改变量?
【发布时间】:2015-12-19 04:00:43
【问题描述】:

我的代码中似乎根本无法增加 currentCost[0] 或 buildingA。

我的问题可能在这里:

{
    document.getElementById("costA").innerHTML = currentCost[0]
    document.getElementById("amountA").innerHTML = buildingA
    document.getElementById("canPay").innerHTML = "You just bought another!"
    count.postMessage({action:'subtract', amount:currentCost[0], once:true});
    currentCost[0] = Math.ceiling(nlogn(currentCost[0]+1)) + 5
    buildingA = buildingA + 1
}

这是在 if 语句中。

我的目标是在购买建筑物时,将现有的该类型建筑物的数量加一,并根据预定函数提高价格,即newPrice = oldPrice*log10(oldPrice)

但是,当我购买A楼时,价格将保持在15,并且总是说我有0个A楼。

【问题讨论】:

  • 您应该在此处发布您的源代码,而不是指向您的整个存储库的链接。这里的代码贴应该是MCVEs

标签: html arrays function


【解决方案1】:

好的,在您的代码中有一行var nlogn = nlogn(); 使nlogn 是一个变量,因此它给出了TypeError: nlogn is not a function。 更改变量名。

并将脚本放在<body>的末尾。

完整代码。

<!DOCTYPE html>
<head>
<title>Test incremental</title>

</head>

<body>
Your money: <output id="result"></output><br><br>

<button onclick="buyA();">Buy Building A</button><br>
Cost of next one: <output id="costA"></output><br>
<output id="canPay"></output><br>
You own <output id="amountA"></output> Building A's!

<script language="JavaScript">

var buyCost = 0;
var buyable = true;
var count;
var buildingModifier = 0;
var cost = 0;
var currentCost = [15, 25, 50, 100]
var money = 0;
//var nlogn = nlogn();

var buildingA = 0;

function beginGame() {
    if(typeof(Worker) !== "undefined") {
        if(typeof(count) == "undefined") {
            count = new Worker("counterWorker.js");
        }
        count.onmessage = function(event) {
            document.getElementById("result").innerHTML = event.data;
            money = event.data
        };
    } else {
        document.getElementById("result").innerHTML = "Sorry, your browser does not support Web Workers...";
    }
}

function nlogn(n) {
    return n * Math.log10(n);
}

function checkBuyable() {
    if(cost < money) {
        buyable = true;
    } else {
        buyable = false;
    }
}

var costA = document.createElement("costA")
var canPay = document.createElement("canPay")
var amountA = document.createElement("amountA")

function buyA() {
    cost = currentCost[0];
    checkBuyable();
    if (buyable == true) {
        document.getElementById("costA").innerHTML = currentCost[0]
        document.getElementById("amountA").innerHTML = buildingA
        document.getElementById("canPay").innerHTML = "You just bought another!"
        count.postMessage({action:'subtract', amount:currentCost[0], once:true});
        currentCost[0] = Math.ceil(nlogn(currentCost[0]+1)) + 5
        buildingA = buildingA + 1
        setTimeout(resetCanPay, 1000)
    } else {
        document.getElementById("canPay").innerHTML = "Too Expensive to buy another!"
        setTimeout(resetCanPay, 1000)
    }
    document.getElementById("costA").innerHTML = currentCost[0]
}

function resetCanPay() {
    document.getElementById("canPay").innerHTML = " "
}

currentCost[0] = Math.ceil(nlogn(currentCost[0]+1))
document.getElementById("costA").innerHTML = currentCost[0]

beginGame();

</script>

</body>

【讨论】:

  • 嗯,还是不行。我仍然可以花 15 块买这栋楼,这不会让我等到 20 多块。
  • 我已经在我的整个代码中声明了 nlogn。那不是问题。编辑:哦,这就是问题所在。我认为它可以区分变量和函数。
  • 再次编辑。请检查。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-06
  • 1970-01-01
  • 2011-05-16
  • 2012-03-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多