Maria: Won’t break terminate the program altogether?
Professor: No, it won’t, because break terminates only the innermost loop in which
it is placed. If that loop is nested within another loop, then the outer loop w ill continue
its execution as though nothing h appened.
Notice how I produced an infinite loop by simply putting the constant value true
in place of the loop condition. Since true always evaluates to true , th e loop can
only be stopped by means of break. By the way, instead of while (t rue) you will
sometimes also see for (;;) which also makes up an infinite loop.
Mike: I suspect that this program can be made shorter, without break, without
number, and using a do/while loop instead of the while loop.
Professor: You know, I think you’re r ight. I was planning to revisit the example later
today anyhow and we may just a s well tr y to make it shorter using your suggestions.
Speaking of break, there ’s a similar statemen t called continue. The dierence is
that continue does not terminate the loop execution completely. It only terminates
the current iteration a nd jumps directly to the loop condition, which is then tested for
the next iteration.
Even though br eak and con tinue can some times make for elegant solutions, you
should limit yourselves to using them exclusively at the start or e nd of the loop body,
only as additional or alternative checks. Otherwise you may later overlook them,
getting yourself in some nasty trouble.
Maria: The drawn numbers produced by your cod e aren’t sorted.
Professor: Of course they aren’t becau se we haven’t implemented any sorting yet.
But we’ll be coming to it sh ortly.
9.4 Array Methods
Professor: Now, you alre ady know that a JavaScript array has a single length prop-
erty, which is automatically updated to always be one larger than the hig hest index of
the array. While array elements are a lso properties, they are usually optimized f or a
significantly faster access th an ordinary properties. There ar e three dierent construc-
tors for the Array object, but they can be replaced by array liter als, and we decided
to use them as they are shorter and there’s no danger of accidentally calling a wron g
constructo r if you use a single numeric argument.
There is more to arrays, though. You’ll discover that the Array objec t implements
quite a few array-manipulating methods. Two of them we are going to meet now and
you’ll find more in the JavaScript reference in section E.3 on page 374. Even what
you find there is just a selection of what this object has in store for the programmer.
For our lucky lottery number generator we ar e going to use two methods, sort() and
indexOf(). The first one is kind of self-expla natory—it simply sorts the array ele-
ments. The elements are reordered in pla ce, which means that no new array is created.
The elements of the array on which you call the sort() metho d are rearranged.
178 Meeting 9. Understanding Arrays and Strings
The indexOf() property searches for an element and retu rns its in dex. If no element
is found, then it returns -1. Here are a couple of examples:
var myArray = [7, 9, 3];
var x = myArray.indexOf(9); //x becomes 1
myArray.sort(); //myArray becomes [3, 7, 9]
x = myArray.indexOf(9); //x becomes 2
Maria: Does sort() only work for numbers?
Professor: As a matter of fact, elements are sorted alphabetically—or according to
the order determined by the character encoding, to be exact. If the elem ents are not
strings, they are converted to strings.
Mike: What abut indexOf() ? Does it also convert e le ments to strings before doing
compariso ns?
Professor: i ndexOf() m akes no conversions. It uses the strict equality operator
(===) for c omparison. The next call, for example, will return -1:
x = myArray.indexOf("9"); //x becomes -1
Maria: What if there’s more than one element equal to the passed a rgument?
Professor: Then the index of the first element that is found is returned. The search
always begins at the beginning of an array. Alternatively, if you want to start a search
from some other position, you can pass an additional argument that specifies an array
index at which to begin the search. The next example uses this possibility to find the
positions of all the elements whose value is 9:
var myArray = [1, 6, 2, 9, 2, 5, 9, 9, 3];
var nines = [];
var index = myArray.indexOf(9);
while (index != -1) {
nines[nines.length] = index;
index = myArray.indexOf(9, index + 1);
}
console.log(nines); //Writes 3, 6, 7
The third line of the co de searches for the first occurrence of the number nine. If
there’s one found, the n the while loop starts. Inside the loop the index of the found
number is first appended a t the end of the ni nes arr ay. Then the next numbe r nine is
searched for. Notice that I entered index + 1 as a start position for the search, which
is one ele ment after the last successful find. If I continued the search at the position
index, then I wou ld be getting the same element over a nd over again.
The same program can be written a little shorter if slightly more enigmatic:
9.4. Array Methods 179
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.16.255.189