简体   繁体   English

js(node.js)while循环不起作用

[英]js (node.js) while loop not working

My node.js code is below. 我的node.js代码如下。

Basically I have a buffer called searchRes and first I get some values from it. 基本上,我有一个名为searchRes的缓冲区,首先我从中获得一些值。

It is supposed to loop round "results - 1" times. 它应该循环“结果-1”次。

It is looping. 它正在循环。

It prints the first 3 bits of information totally correctly and then after that (4th onwards) is always the same as the 3rd one. 它会完全正确地打印前3位信息,然后(第4位起)始终与第3位相同。

I have no idea why. 我不知道为什么。

results = 20;
var sSize = searchRes.slice(8,16);
sSize = parseInt(sSize,16);
lols = new Array();

while(num <= results -1 ){

    var cur = num + 1;
    var sres=0;
    for(var i in lols) { sres += lols[i]; }

    if (num == 0){

        var clanRes = searchRes.slice(0, 128 + (sSize * 2));

        var cLen = clanRes.slice(8,16);
        cLen = parseInt(cLen,16);

        var firstLen = clanRes.length;
        var lastLen = clanRes.length;

    }else{

        var cLen = searchRes.slice(lastLen + 8, lastLen + 16);

        cLen = parseInt(cLen,16);

        var clanRes = searchRes.slice(
            lastLen, 
            lastLen + 128 + (cLen * 2)
        );

            var abc = 1;
            if (abc == 1){
                var lastLen = firstLen + clanRes.length;
                abc++;
            }else{
                var lastLen = lastLen + clanRes.length;
            }

    }


    lols.push(cLen);

    console.log('\n\nClan ' + cur + ' Details');

    var clanID = clanRes.slice(0,8);
    clanID = parseInt(clanID,16);

    num ++;
}

I'm not sure at all about where the problem is. 我根本不知道问题出在哪里。 Is it when defining clanRes or is it in making the loop? 是在定义clanRes时还是在进行循环时?

Thanks! 谢谢!

** **

  • Updated code for @bergi @bergi的更新代码

** **

num = 0;
var sSize = searchRes.slice(8,16);
sSize = parseInt(sSize,16);
lols = new Array();

    while(num <= 3){

        var cur = num + 1;
        var sres=0;
        for(var i in lols) { sres += lols[i]; }

        if (num == 0){

            var clanRes = searchRes.slice(0, 128 + (sSize * 2));

            var cLen = clanRes.slice(8,16);
            cLen = parseInt(cLen,16);

            var firstLen = clanRes.length;
            var lastLen = clanRes.length;

        }else{

            var cLen = searchRes.slice(lastLen + 8, lastLen + 16);

            cLen = parseInt(cLen,16);

            var clanRes = searchRes.slice(
                lastLen, 
                lastLen + 128 + (cLen * 2)
            );

                var abc = 1;
                if (abc == 1){
                    var lastLen = firstLen + clanRes.length;
                    abc++;
                }else{
                    var lastLen = lastLen + clanRes.length;
                }

        }


        lols.push(cLen);

        console.log('\n\nClan ' + cur + ' Details');

        var clanID = clanRes.slice(0,8);
        clanID = parseInt(clanID,16);
        console.log(clanID);

        num ++;
    }

Here is a exmaple searchRes: http://pastie.org/10075399 这是一个出色的搜索结果: http : //pastie.org/10075399

And an example sSize: 0000000e - hex 14 - int. 还有一个示例sSize:0000000e-十六进制14-int。

 var abc = 1; if (abc == 1){ 

abc is always 1 in that comparison (the value is assigned right in the line before), so it will never evaluate the else block. abc在该比较中始终为 1 (该值在前面的行中分配),因此它将永远不会评估else块。 I assume you want to move the initialisation of the variable out of the looping. 我假设您想将变量的初始化移出循环。

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

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