Java Reference
In-Depth Information
Passing Arguments to jjs
The following is an example of passing arguments to the jjs tool. The first five natural
numbers are passed to the jjs tool as arguments and they are accessed using the
arguments property later. Notice that you must add a space between the two hyphens and
the first argument:
c:\>jjs -- 1 2 3 4 5
jjs> for each (x in arguments ) print(x)
1
2
3
4
5
jjs> quit()
c:\>
Consider the script in Listing 10-1. The script has been saved in a file named
stream.js . The script works on a list of integers. The list can be passed to the script as
the command-line arguments. If the list is not passed as arguments, it uses the first five
natural numbers as the list. It computes the sum of the squares of odd integers in the list.
It prints the list and the sum.
Listing 10-1. A Script to Compute the Sum of the Squares of Odd Integers in a List
// stream.js
var list;
if (arguments.length == 0) {
list = [1, 2, 3, 4, 5];
}
else {
list = arguments;
}
print("List of numbers: " + list);
var sumOfSquaredOdds = list.filter(function(n) {return n % 2 == 1;})
.map(function(n) {return n * n;})
.reduce(function(sum, n) {return sum + n;}, 0);
print("Sum of the squares of odd numbers: " + sumOfSquaredOdds);
 
Search WWH ::




Custom Search