For loop with node.js / nightbot code?

Hi, I cant seem to get nightbot to use a simple for loop? Is it capable of doing so?
Thanks

If you’re talking about the $(eval) variable, you can use a for just fine. Just make sure the total execution time of your code is less than 1 second.

Would help if you also posted your code here, but make sure to remove/censor any tokens and such if you’re using APIs.

Lets just say for example. $eval( for(var i = 0;i<25;i++) { “printExample” } ).

Is this the right use if eval?

I mean… it works, just not how you’d expect.

It won’t print out 25 of "printExample", it’ll just print out once (for reasons I don’t really know how to explain).

I’m not sure what you’re trying to do, but here’s an example on what you could do for it to track the i for each iteration of the loop: $(eval let text = ''; for (let i = 0; i < 25; i++) {text += i + ', '}) - resulting in: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,

1 Like

Thanks. Yea I noticed that it just prints the value of the max iteration. What I really want to do is find the largest value of an array of values in an array. Something like.
$(eval api=$(urlfetch json https://app.sbmmwarzone.com/player/match?username=ohcoyle%232367&platform=battle);
k=api[1].kills; a=api[2].kills; b=api[3].kills; c=api[4].kills; d=api[5].kills; e=api[6].kills; f=api[7].kills; g=api[8].kills; h=api[9].kills; i=api[10].kills;
kk=api[11].kills; aa=api[12].kills; bb=api[13].kills; cc=api[14].kills; dd=api[15].kills; ee=api[16].kills; ff=api[17].kills; gg=api[18].kills; hh=api[19].kills; ii=api[20].kills
var largest = k;
var array = [k,a,b,c.d,e,f,g,h,i,kk,aa,bb,cc,dd,ee,ff,gg,hh];
for(var i = 0; i < 20; i++)
{
if (array[i] > largest)
{
largest = array[i];
}
}
‘$(largest)’
)

I should have been more clear at the start sorry. Thanks for any help you can give!

If the only thing you want to do is get highest amount of kills in the last few matches, then this should work:
$(eval const api = $(urlfetch json https://app.sbmmwarzone.com/player/match?username=ohcoyle%232367&platform=battle); const kills = api.map(x => x.kills); Math.max(...kills))

For a quick explanation:

  • api is just the API response - like you already had
  • kills = api.map(x => x.kills) creates an array with just the kill numbers, such as: [4,5,2,2,6,5,8,13,21,4,5,4,0,1,3,20,2,16,2,21] - See Array.map()
  • Math.max(...kills) takes the array, uses the “spread operator” to split all numbers into function parameters, Math.max() then finds the highest number of all the input numbers.
1 Like

Very cool thanks very much. I usually work in c++ this node js stuff doesn’t treat me well. Thanks for the explanations they help a lot! Would the code I typed above work roughly as expected?

Yes, but there are syntax errors. I’ll break it down quickly:

$(eval 
    api=$(urlfetch json https://app.sbmmwarzone.com/player/match?username=ohcoyle%232367&platform=battle);
    k=api[1].kills; a=api[2].kills; b=api[3].kills; c=api[4].kills; d=api[5].kills; e=api[6].kills; f=api[7].kills; g=api[8].kills; h=api[9].kills; i=api[10].kills;
    kk=api[11].kills; aa=api[12].kills; bb=api[13].kills; cc=api[14].kills; dd=api[15].kills; ee=api[16].kills; ff=api[17].kills; gg=api[18].kills; hh=api[19].kills; ii=api[20].kills

    var largest = k;
    // there's a typo between `c` and `d`, a dot instead of a comma
    var array = [k,a,b,c.d,e,f,g,h,i,kk,aa,bb,cc,dd,ee,ff,gg,hh];
    for(var i = 0; i < 20; i++)
    {
        if (array[i] > largest)
        {
            largest = array[i];
        }
    }
    // this isn't valid syntax at all, you're probably trying to do:
    // `${largest}`
    // 
    // even so it's unnecessary, you could just put the variable name and nothing else:
    // largest
    ‘$(largest)’
)

If you want to do it without using fancy JavaScript methods, then the following would’ve worked just as well:

$(eval 
    api=$(urlfetch json https://app.sbmmwarzone.com/player/match?username=ohcoyle%232367&platform=battle);

    var largest = 0;
    for(var i = 0; i < 20; i++)
    {
        if (api[i].kills > largest)
        {
            largest = array[i].kills;
        }
    }

    largest
)

No need to define variables for each kills parameter in the response array.
The last largest would make Nightbot print out the value of largest

Since we’re also working with JavaScript, another method is just looping over the array elements instead (including this one for fun):

$(eval 
    api=$(urlfetch json https://app.sbmmwarzone.com/player/match?username=ohcoyle%232367&platform=battle);

    let largest = 0;
    for (const match of api)
    {
        if (match.kills > largest)
        {
            largest = match.kills;
        }
    }

    largest;
)

Any of the ways I posted (besides your code which I just formatted and added comments to) should work for your purpose. Hopefully that clears that up a bit.

1 Like

It sure did thanks a lot. That’s very interesting. Yea not iterating through the api array was very silly hahah. Thanks again for taking your time to give me a lesson!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.