Unlike written text, computer programs do not have to simply run from top to bottom. Loops do something a predefined number of times. When the computer reaches the beginning of a Loop, it checks how many times it's supposed to run. It will then run to the bottom of the Loop, jump back to the top and continue that cycle until it has satisfied the specified number of iterations. Once it has done this, it continues on to the next line of code, below.
There are two main types of Loops that you will be using for your translator. "For" Loops and "While" Loops. "For" Loops do something a specific number of times. You might have a Loop run once for each letter in your name. In my case: "Adam" would run four times then stop. A "While" Loop runs as long as a certain condition is true. "While today is Thursday" would run as long as today is still Thursday.
Create the following For Loop.
Example 9.1
var x = 2;
for (var i=0; i < 5; i++) {
x++;
}
Zotero.debug(x);
With any luck, you should get:
Example 9.2
===>7<===(number)
That's because the Loop ran five times, increasing the value of x by one each time.
For Loops take three Arguments, each separated by a semicolon.
For Loops are quite handy when working with Arrays, XPaths and other objects. If you know your XPath grabbed ten nodes and you want to save each into Zotero, you can use a For Loop which performs the same operations on each of those nodes, rather than writing the code out for each node.
To use a Loop to do something to all the elements in an Array, use the Loop counter to point to each item. For example:
Example 9.3
var myArray = new Array("a", "b", "c", "d", "e", "f", "g");
for (var i = 0; i < myArray.length; i++) {
myArray[i] = myArray[i] + "1";
Zotero.debug(myArray[i]);
}
Zotero.debug(myArray);
This will Loop through once for each item in myArray and Concatenate a "1" to it. Since the counter starts at zero and increased by one each time the Loop executes, you can use the counter to point to each successive item in the Array and change it. To do this without a Loop would require many more lines of code.
Paste the above code into Scaffold and run it to see how Loops work.
Assuming you got your syntax correct and your Array had more than a couple items in it, you should notice that Zotero.debug(x) gave you multiple values in the window at the bottom of Scaffold. You have now created a Loop and you have learned how to see what happens as the Loop runs through each of its iterations.
The Loop you've just created isn't very interesting, but it can contain any number of Methods, Variables, math, If Statements and lines of code. Everything you've learned so far can go inside a For Loop, including other For Loops.
While Loops are a lot like For Loops, except instead of running a set number of times, they run as long as a certain condition is true.
Try the following code
Example 9.4
var x = 3;
while (x != 6){
x++;
Zotero.debug(x);
}
x = x + 3;
Zotero.debug(x);
This Loop will run as long as x is not equal to 6. As soon as x changes to 6, the Loop will stop and the program will continue reading below. The final value of x should be 9 in this case. Like a For Loop, While Loops can contain any combination of other Loops, Statements, Methods, math and Variables.
Declare an Array and fill it with integers. Then, create a For Loop that will add the String " is an odd number" or " is an even number" appropriately to each number in the Array. If you need a hint on how to do this, look back to the Math section in Chapter 7.