简体   繁体   中英

js (node.js) while loop not working

My node.js code is below.

Basically I have a buffer called searchRes and first I get some values from it.

It is supposed to loop round "results - 1" times.

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.

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?

Thanks!

**

  • Updated code for @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

And an example sSize: 0000000e - hex 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. I assume you want to move the initialisation of the variable out of the looping.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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