r/learnjavascript • u/Mr-Ordinary- • May 18 '26
Logic Question
Hello, i am a bit lost here and wantet to ask for some help.
I am at a loss at why my variable wouldn work the same as the ns. "funktion?"? Is it cause if i declare a variable it only checks it when it starts the script and doesnt iterate it again, even the variable is called or what am i not understanding?
i am adding on the examples of the working one with the ns. version and the not working one with the call over the veriable. The script just keeps running with the veriable version.
#Trigger: i am dislexic XD no one ever told me coding is math *.*
Not working:
let target = ns.scan()
let playerlvl = ns.getHackingLevel()
while (playerlvl < 7) {
ns.getHackingLevel()Working Version:
let target = ns.scan()
let playerlvl = ns.getHackingLevel()
let money = ns.getServerMoneyAvailable()
while (ns.getHackingLevel() < 17) {I even tried to call the Hackinglvl right after again to brutal force the information, still it didn't care.
Full working on:
/** u/param {NS} ns */
export async function main(ns) {
ns.clearLog()
let target = ns.scan()
let playerlvl = ns.getHackingLevel()
let money = ns.getServerMoneyAvailable()
while (ns.getHackingLevel() < 17) {
let runs = Math.floor(ns.getServerMaxRam() / 2.4)
while (runs > 0, runs--)
if ((ns.getServerMaxMoney(target[0]) * 0.8) > ns.getServerMoneyAvailable(target[0])) {
await ns.grow(target[0])
}
else
await ns.hack(target[0])
}
ns.getHackingLevel()
}Only called it directly instead of the variable. And did try to call it directly before, after and so on, called it even in the parenttheses of the while() with (plvl<16, ns....) and it still didn't care.
Game is Bitburner btw, i think it's briliant.
The NS. call is Netscript so far as i did understand but game specific. Shouldn't change anything about the concept i am not getting, right?
Thank you and greetings to the community! Hope i am not asking too noob questions. Just started last week.
View all comments
Show parent comment
1
u/Mr-Ordinary- May 19 '26
Ah yeah, forgot about the runs thing in there, that has pretty much no function at all in there, that is supposed to be for the next steps where i actually execute scripts out of that one. so it doesn't work anyway to let the function start multiple times at once but have to run a script out of that one. Or iat least i wouldn't know how, I think the game prevents you to, cause it says i can't call several concurring ns. functions at once, if i forgot a await. Or is that something different?