简体   繁体   English

获得无限循环,无法理解为什么 - Javascript

[英]Getting an infinite loop and can't see why - Javascript

I'm writing a simple little Connect 4 game and I'm running into an infinite loop on one of my functions: 我正在写一个简单的小连接4游戏,我在我的一个函数上遇到了无限循环:

var reds = 0;
var greens = 0;

function checkEmpty(div) {
    var empty = false;
    var clicked = $(div).attr('id');
    console.log(clicked);
    var idnum = parseInt(clicked.substr(6));
    while (idnum < 43) {
        idnum = idnum + 7;
    }
    console.log("idnum=" + idnum);
    while (empty == false) {
        for (var i = idnum; i > 0; i - 7) {
            idnumStr = idnum.toString();
            var checking = $('#square' + idnumStr);
            var str = checking.attr('class');
            empty = str.includes('empty');
            console.log(empty);
            var divToFill = checking;
        }
    }

    return divToFill;
}

function addDisc(div) {
    if (reds > greens) {
        $(div).addClass('green');
        greens++;
        console.log("greens=" + greens);
    } else {
        $(div).addClass('red');
        reds++;
        console.log("reds=" + reds);
    };
    $(div).removeClass('empty');
}

$(function() {
    var i = 1;
    //add a numbered id to every game square
    $('.game-square').each(function() {
        $(this).attr('id', 'square' + i);
        i++;
        //add an on click event handler to every game square

        //onclick functions
        $(this).on('click', function() {
            var divToFill = checkEmpty(this);
            addDisc(divToFill);
        })
    })
})

Here is a link to the codepen http://codepen.io/Gobias___/pen/xOwNOd 这是codepen http://codepen.io/Gobias___/pen/xOwNOd的链接

If you click on one of the circles and watch the browser's console, you'll see that it returns true over 3000 times. 如果您单击其中一个圈子并观察浏览器的控制台,您将看到它返回true超过3000次。 I can't figure out what I've done that makes it do that. 我无法弄清楚我做了什么让它做到了。 I want the code to stop as soon as it returns empty = true. 我希望代码在返回empty = true时立即停止。 empty starts out false because I only want the code to run on divs that do not already have class .green or .red. empty开始出错,因为我只希望代码在没有类.green或.red的div上运行。

Where am I going wrong here? 我在哪里错了?

for (var i = idnum; i > 0; i - 7); for(var i = idnum; i> 0; i - 7);

You do not change the i . 你不改变i

Do you want to decrement it by 7? 你想减少7吗?

Change your for loop to the one shown below: for循环更改for如下所示:

for (var i = idnum; i > 0; i -= 7) {
  // ...
}

You also do not use loop variable in the loop body. 您也不在循环体中使用循环变量。 Instead, you use idnum , I think this can be issue. 相反,你使用idnum ,我认为这可能是问题。

while (empty == false) {
    for (var i = idnum; i > 0; i -= 7) {
        idnumStr = i.toString(); // changed to i
        var checking = $('#square' + idnumStr);
        var str = checking.attr('class');
        empty = str.includes('empty');
        console.log(empty);
        var divToFill = checking;
        // and don't forget to stop, when found empty
        if (empty) break;
    }
}

I add break if empty found, because if we go to next iteration we will override empty variable with smallest i related value. 我添加break如果找到空,因为如果我们进入下一次迭代,我们将覆盖具有最小i相关值的空变量。

You can also wrap empty assignment with if (!empty) {empty = ...;} to prevent this override, but I assume you can just break, because: 你也可以使用if (!empty) {empty = ...;}来包装empty赋值以防止这种覆盖,但我认为你可以破解,因为:

I want the code to stop as soon as it returns empty = true 我希望代码在返回empty = true时立即停止


Offtop hint: Offtop提示:

while (idnum < 43) {
    idnum = idnum + 7;
}

can be easy replaced with: idnum = 42 + (idnum%7 || 7) 可以很容易地替换为: idnum = 42 + (idnum%7 || 7)

Change to this: 改为:

for (var i = idnum; i > 0; i = i - 7) {

You are not decrementing the i in your for loop 你没有在你的for循环中递减i

Building on what the others have posted You would want to change the value of empty inside the for loop. 基于其他人发布的内容你想要在for循环中更改empty的值。 because obviously the string still checks the last string in the loop which would always return false. 因为很明显,字符串仍会检查循环中的最后一个字符串,该字符串总是返回false。

while(empty==false){

for (var i = idnum; i > 0; i -= 7) {
   // your other codes
  if (!empty) {
   empty = str.includes('empty');
   }
}

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

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