0

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.

  • Can you please supply example values for `searchRes` and `sSize`? – Bergi Apr 05 '15 at 20:42
  • There are multiple problems with your code. `num` is initialised nowhere, you [use `for in` loops on an array](http://stackoverflow.com/q/500504/1048572), you do `var abc = 1; if (abc == 1)`, variables such as `clanID` are unused, `lastLen` is used before initialised… Please fix those first, or explain how you expected them to work. – Bergi Apr 05 '15 at 20:46
  • @Bergi Ok. Num is initalized as 0 - i've missed out out though. The loop is working for me. ClanID is stored, I will need it later. lastLen is defined first. num will always == 0 on first run so `var lastLen = clanRes.length;` will run. I don't get what is wrong with the abc value. Anyway I've posted a new version of the code. –  Apr 05 '15 at 20:57
  • `abc` is *always* `1` in that comparison (the value is assigned right in the line before), so it will never evaluate the `else` block. – Bergi Apr 05 '15 at 21:00
  • @Bergi thanks - i'm checking if that fixed the problem - it might have. –  Apr 05 '15 at 21:02
  • @Bergi fixed it! Feel free to post as answer :) –  Apr 05 '15 at 21:03

1 Answers1

0
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.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375