From d855eb630150311205ee1664a3ee6dd633df7d09 Mon Sep 17 00:00:00 2001 From: MrNuggelz Date: Wed, 28 Jun 2017 18:06:31 +0200 Subject: [PATCH] update netscript documentation --- src/Constants.js | 70 +++++++++++++----------------------------------- 1 file changed, 18 insertions(+), 52 deletions(-) diff --git a/src/Constants.js b/src/Constants.js index 1ca6a45c9..0565bdc00 100644 --- a/src/Constants.js +++ b/src/Constants.js @@ -342,41 +342,7 @@ CONSTANTS = { " ==
" + " !=

" + "

Arrays


" + - "Arrays are special container objects. Arrays can hold many values under a single name. Each value in the array " + - "can be accessed using an index number. The following example shows how to declare an array:

" + - "thisIsAnArray = Array[1, 2, 3, 'bitburner!', false];

" + - "Note that the values in an array can be different types. To access this array we just declared, we can use the index " + - "operator on the array's name:

" + - "print(thisIsAnArray[0]);
" + - "thisIsAnArray[1] = 5;
" + - "thisIsAnArray[3] = 'string concatenation ' + 123;

" + - "Note that arrays are indexed starting at index 0. Using an index that is too large or less than 0 will result in an " + - "out of bounds runtime error.

" + - "If an element in an array is assigned to a value that includes a variable, then it holds a reference to that variable. " + - "What this means is that if the variable changes, the array element will also change accordingly. For example:

" + - "x = 10;
testArr = Array[x];
print(testArr[0]);
x = 20;
print(testArr[0]);

" + - "This code will print:

10
20

" + - "Array functions
" + - "Arrays have built-in functions/properties that can be used to more easily access and manipulate the containers.

"+ - "length/length()
Returns the number of elements in the array.
" + - "The example below will print out 5:

" + - "arr = Array[1, 2, 3, 4, 5];
print(arr.length);

" + - "clear/clear()
Removes all elements from the array.
" + - "The example below creates an array with three strings and then uses clear to remove all of those strings. The result is that 'arr' will be " + - "an empty array.

" + - "arr = Array['str1', 'str2', 'str3'];
arr.clear();

" + - "push(e)
Adds the element e to the end of the array.
" + - "The example below will create an array holding one element: the number 1. It will then push the number 2 onto the array. The result " + - "is that 'arr' will be an array of size 2 with arr[0] == 1 and arr[1] == 2

" + - "arr = Array[1];
arr.push(2);

" + - "insert(e)
Inserts an element e into an array at a specified index. Every element in the array that is at or after " + - "the specified index is shifted down. The array must be indexed with the [] operator when using this function.
" + - "The following example will insert the number 2 into index 1 of the array. The result afterwards is that 'arr' will hold the values [1, 2, 3, 4].

" + - "arr = Array[1, 3, 4];
arr[1].insert(2);

" + - "remove()
Removes an element from a specified index. Every element in the array that is after the specified index " + - "will be shifted up. The array must be indexed with the [] operator when using this function.
" + - "The following example will remove the first element of the array. The result afterwards is that 'arr' will hold the values [2, 3].

" + - "arr = Array[1, 2, 3];
arr[0].remove();

" + + "Netscript arrays have the same properties and functions as javascript arrays. For information see javascripts array documentation.

"+ "

Script Arguments


" + "Arguments passed into a script can be accessed using a special array called 'args'. The arguments can be accessed like a normal array using the [] " + "operator. (args[0], args[1], args[2]...)

" + @@ -528,51 +494,51 @@ CONSTANTS = { "hacknetnodes[i].cores
Returns the number of cores on the corresponding Hacknet Node

" + "hacknetnodes[i].upgradeLevel(n)
Tries to upgrade the level of the corresponding Hacknet Node n times. The argument n must be a " + "positive integer. Returns true if the Hacknet Node's level is successfully upgraded n times or up to the max level (200), and false otherwise.

" + - "hacknetnodes[i].upgradeRam()
Tries to upgrade the amount of RAM on the corresponding Hacknet Node. Returns true if the " + + "hacknetnodes[i].purchaseRamUpgrade()
Tries to upgrade the amount of RAM on the corresponding Hacknet Node. Returns true if the " + "RAM is successfully upgraded, and false otherwise.

" + - "hacknetnodes[i].upgradeCore()
Attempts to purchase an additional core for the corresponding Hacknet Node. Returns true if the " + + "hacknetnodes[i].purchaseCoreUpgrade()
Attempts to purchase an additional core for the corresponding Hacknet Node. Returns true if the " + "additional core is successfully purchase, and false otherwise.

" + "Example: The following is an example of one way a script can be used to automate the purchasing and upgrading of Hacknet Nodes. " + "This script purchases new Hacknet Nodes until the player has four. Then, it iteratively upgrades each of those four Hacknet Nodes " + "to a level of at least 75, RAM to at least 8GB, and number of cores to at least 2.

" + "while(hacknetnodes.length < 4) {
" + "    purchaseHacknetNode();
" + - "};
" + - "for (i = 0; i < 4; i = i+1) {
" + + "}
" + + "for (i = 0; i < 4; i = i++) {
" + "    while (hacknetnodes[i].level <= 75) {
" + "        hacknetnodes[i].upgradeLevel(5);
" + "        sleep(10000);
" + - "    };
" + - "};
" + - "for (i = 0; i < 4; i = i+1) {
" + + "    }
" + + "}
" + + "for (i = 0; i < 4; i = i++) {
" + "    while (hacknetnodes[i].ram < 8) {
" + "        hacknetnodes[i].upgradeRam();
" + "        sleep(10000);
" + - "    };
" + - "};
" + - "for (i = 0; i < 4; i = i+1) {
" + + "    }
" + + "}
" + + "for (i = 0; i < 4; i = i++) {
" + "    while (hacknetnodes[i].cores < 2) {
" + "        hacknetnodes[i].upgradeCore();
" + "        sleep(10000);
" + - "    };
" + - "};

" + + "    }
" + + "}

" + "

While loops


" + "A while loop is a control flow statement that repeatedly executes code as long as a condition is met.

" + "while ([cond]) {
    [code]
}


" + "As long as [cond] remains true, the code block [code] will continuously execute. Example:

" + - "i = 0;
while (i < 10) {
    hack('foodnstuff');
    i = i + 1;
};


" + + "i = 0;
while (i < 10) {
    hack('foodnstuff');
    i = i + 1;
}


" + "This code above repeats the 'hack('foodnstuff')' command 10 times before it stops and exits.

" + - "while(true) {
     hack('foodnstuff');
};


" + + "while(true) {
     hack('foodnstuff');
}


" + "This while loop above is an infinite loop (continuously runs until the script is manually stopped) that repeatedly runs the 'hack('foodnstuff')' command. " + "Note that a semicolon is needed at closing bracket of the while loop, UNLESS it is at the end of the code

" + "

For loops


" + "A for loop is another control flow statement that allows code to be repeated by iterations. The structure is:

" + - "for ([init]; [cond]; [post]) {
    code
};


" + + "for ([init]; [cond]; [post]) {
    code
}


" + "The [init] expression evaluates before the for loop begins. The for loop will continue to execute " + "as long as [cond] is met. The [post] expression will evaluate at the end of every iteration " + "of the for loop. The following example shows code that will run the 'hack('foodnstuff');' command 10 times " + " using a for loop:

" + - "for (i = 0; i < 10; i = i+1) {
    hack('foodnstuff');
};


" + + "for (i = 0; i < 10; i = i++) {
    hack('foodnstuff');
}


" + "

If statements


" + "If/Elif/Else statements are conditional statements used to perform different actions based on different conditions:

" + "if (condition1) {
    code1
} elif (condition2) {
    code2
} else {
" + @@ -584,7 +550,7 @@ CONSTANTS = { "Note that a conditional statement can have any number of elif statements.

" + "Example:

" + "if(getServerMoneyAvailable('foodnstuff') > 200000) {
    hack('foodnstuff');
" + - "} else {
    grow('foodnstuff');
};

" + + "} else {
    grow('foodnstuff');
}

" + "The code above will use the getServerMoneyAvailable() function to check how much money there is on the 'foodnstuff' server. " + "If there is more than $200,000, then it will try to hack that server. If there is $200,000 or less on the server, " + "then the code will call grow('foodnstuff') instead and add more money to the server.

",