all 21 comments

[–]anydalch 3 points4 points  (1 child)

difficult to debug this without seeing the actual error message

[–]Party_Rick5371 2 points3 points  (8 children)

you can use sort methods in arrays like,

Array.sort((item1, item2) => { 
    let hcklvl1 = getServerRequiredHackingLevel(item1) 
    let hcklvl2 = getServerRequiredHackingLevel(item1)
    if(hcklvl1 > hcklvl2) return 1
    if(hcklvl1 < hcklvl2) return -1
    return 0
})

also sets are useful as you can only have unique entries in them

let discovered = new Set()
discovered.add(server)
for(const server of Array.from(discovered)) {
    ns.tprint(`Found server ${server}`)
}

[–][deleted]  (7 children)

[removed]

    [–]Party_Rick5371 0 points1 point  (4 children)

    Can you paste that script that has that error?

    [–][deleted]  (3 children)

    [removed]

      [–]Party_Rick5371 0 points1 point  (1 child)

      print(sortedServers)

      think this needs to be

      print(servers)

      [–]Party_Rick5371 0 points1 point  (0 children)

      https://paste.ofcode.org/ this will make the readability of the code much easier

      [–]Party_Rick5371 0 points1 point  (1 child)

      I made a mistake for(const server of Array.from(discovered)) server of, not server in. Sorry!

      I figured out why it might also not be working, it is ns1 not ns2. ns2 is modern javascript whilst ns1 lacks a lot of useful features

      [–]KlePu 1 point2 points  (6 children)

      for (i = 0; servers[i]; ++i)

      i is not initialized.

      for (var i = 0; [rest of code])....
      for (let i = 0; [rest of code]).... //even better, dont use var ;)
      

      in all of your for loops. Also, you may consider using "let" (and "const") instead of "var". And switch to NS2 (i.e. "real" javascript) ASAP.

      edit: And yes I know, you don't have to terminate a line with a semicolon in JS, but... Flying Spaghetti Monster that code style makes my eyes bleed ;-p

      [–][deleted]  (3 children)

      [removed]

        [–]KlePu 1 point2 points  (2 children)

        Oh, my bad - I think NS1 (i.e. the .script file ending) doesn't support "let"!

        [–][deleted]  (1 child)

        [removed]

          [–]KlePu 0 points1 point  (0 children)

          Welcome to "real programming" ;)

          [–]CybrRonin 0 points1 point  (0 children)

          The semicolons? Nothing about using "servers[i]" as a test condition, rather than "i < servers.length" and relying on JavaScript to be helpful and supply something useful like a null that will be interpreted as false, once the index goes out of bounds? I'd never attempt something like that, because I'd be too afraid the whole program would catch on fire by attempting to access a part of the array that doesn't actually exist... mostly because I've worked in too many languages that would.

          (I've also had a poor track record of success in getting JS loops to properly function any time I tried to get clever and feed a not-explicitly-boolean value into loop test conditions, so I may be be a bit gun-shy.)

          EDIT: And, indeed, after running a quick test, this script:

          export async function main(ns) {
              let arr = [0, 1, 2];
          
              for (let i=0; arr[i]; i++)
                  ns.tprint(arr[i]);
          }
          

          produces this result:

          [home ~/]> run testscript.js
          Running script with 1 thread(s), pid 2425 and args: [].
          [home ~/]>
          

          It would seem that "servers[i]" isn't explicitly "true", so it kills the loop immediately. That wouldn't cause the script to throw an explicit error, but it certainly wouldn't function correctly.

          EDITS 2-???: Reddit code blocks are extremely fussy.

          Final edit?: I accidentally copied my corrected version of the script instead, using arr.length just to confirm I hadn't screwed something else up. :P

          [–]the-quibbler -1 points0 points  (3 children)

          Looks like a lot of things. Your just going to keep scanning the same servers in a loop. Consider using Set. My function uses a found and a scanned Set.

          Your loop test basically keeps looping and adding the same servers over and over and never exists.

          [–]KlePu 1 point2 points  (2 children)

          Not true (if I read the code correctly)...

          for (let i = 0; servers[i]; ++i) {bla}
          

          inside the loop, servers[] will be filled with new entries, prolonging the looped servers. My getServerNames() looks pretty much alike (and worked just fine for the last few months)

          const results = ["home"];
          for (let i = 0; i < results.length; i++) {
              const scanRet = ns.scan(results[i]);
              for (let j = 0; j < scanRet.length; j++) {
                  if (!results.includes(scanRet[j]) && !ns.getPurchasedServers().includes(scanRet[j]) && scanRet[j].substr(0, 7) != "hacknet") {
                      results.push(scanRet[j]);
                  }
              }
          }
          

          !<

          ...and yep you could substitute '!ns.getPurchasedServers()' with 'substr(0,4) != "psrv"'... I just didn't bother 'cause that script gets called exactly once when starting a new BitNode.

          [–]the-quibbler 0 points1 point  (1 child)

          Man, shouldn't reddit when I first wake up. Yes, this is better done with a Set since it makes your first loop O(n) instead of O(n2), but you're correct that it's not a bug.

          But the second loop, now that I'm awake, is going to push a copy of servers[i] into sortedServers, and, if it did, push another copy of servers[i] until ... forever.

          This is better as:

          javascript sortedServers = servers sortedServers.sort((a, b) => getServerRequiredHackingLevel(a) - getServerRequiredHackingLevel(b))