使用`for`循环导致游戏崩溃时单击按钮运行循环

时间:2017-12-10 00:57:43

标签: javascript

试图获得一些数字(var信念和小饰品)每秒上升(使用for循环),我遇到错误“期望一个条件表达式但是看到一个赋值”当使用时通过以下代码分析函数:

function trinketCount(number) {
    if(towers >= 1) {
        var trinketCap = 500;
        for(; trinkets = trinketCap;) {
            trinkets = trinkets + (towers * 2);
        }
        document.getElementById('trinkets').innerHTML = trinkets;
    }
}

以及以下非常相似的循环:

function beliefCount(number) {
    if(towers >= 1) {
        var beliefCap = 600;
        for(; belief = beliefCap;) {
            belief = belief + towers;
            document.getElementById('belief').innerHTML = belief;
        }
    }
}

单击按钮构建“塔”也会导致整个事件崩溃。 我对js相当新,所以如果问题很明显,我道歉!也请在https://codepen.io/dirtbf/project/full/XjvdMX

自行查看

1 个答案:

答案 0 :(得分:0)

"expected a conditional expression but instead saw an assignment"

这正是问题所在。

for(; trinkets = trinketCap;) {

for(; belief = beliefCap;) {

您在这里分配值而不是检查它们是否相等。该错误是否带有行号?

固定功能版本:

function trinketCount(number) {
    if(towers >= 1) {
        var trinketCap = 500;
        for(; trinkets == trinketCap;) {
            trinkets = trinkets + (towers * 2);
        }
        document.getElementById('trinkets').innerHTML = trinkets;
    }
}

function beliefCount(number) {
    if(towers >= 1) {
        var beliefCap == 600;
        for(; belief = beliefCap;) {
            belief = belief + towers;
            document.getElementById('belief').innerHTML = belief;
        }
    }
}