How to Write a Zotero Translator:
A Practical Beginners Guide for Humanists

By: Adam Crymble

 

Chapter 9: JavaScript Loops

 previous button  next button

This Chapter

For Loops

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.

For Loop Syntax

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.

  1. var i = 0;
    The first Argument sets the initial value of the Variable, in this case the counter, that counts through the iterations in the Loop. This should be a Number. If you have not declared this Variable previously in your code be sure to include var. You can import a value from another Variable to use as your counter. For instance, i = x; for the above problem
  2. i < 5;
    The second Argument tells the computer what the condition must be for the Loop to continue. This is exactly the same as the Argument you put into your If Statements in the previous chapter. In this case, if i is less than 5 the Loop will continue. You could use any of the comparisons used for If Statements for this Argument; ie., i<= x.
  3. i++
    The final Argument changes the value of your counter with each iteration of the Loop. In the example above, the counter will increase by one each time the Loop runs. You could use any mathematical equation here; ie., i = i – 3 * 2 . However, a simple i++ is most common.

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.

Practice:

  1. Declare and fill an Array with multiple values (at least 5).
  2. Declare a simple Variable x which contains the length of the Array.
  3. Declare a For Loop that uses i as its counter and give it the value of x.
  4. Set a condition using Less Than.
  5. Set the For Loop to iterate by 2 each pass.
  6. Inside the Loop, type Zotero.debug(x);
  7. Execute the code.

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 Loop

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.

While Loop Syntax

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.

Practice:

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.

What you should understand before moving on

Further Reading