14.08.2016 Views

Beginning JavaScript with DOM Scripting and Ajax, 2nd Edition

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 2 ■ Data <strong>and</strong> Decisions<br />

document.write(arrayToSort[3] + "" );<br />

document.write(arrayToSort[4] + "" );<br />

<br />

<br />

<br />

The items are arranged like this:<br />

Apple<br />

Banana<br />

Cabbage<br />

Lemon<br />

Pear<br />

If, however, you lower the case of one of the letters—the A of Apple, for example—then you’ll end up <strong>with</strong> a<br />

very different result. The sorting is strictly mathematical—by the number of the character in the ASCII set, not like a<br />

human being would sort the words.<br />

If you want to change the order in which the sorted elements are displayed, you can use the reverse() method to<br />

display the last one in the alphabet as the first element:<br />

<br />

var arrayToSort = new Array( "Cabbage", "Lemon", "Apple", "Pear", "Banana" );<br />

arrayToSort.sort( );<br />

sortedArray.reverse( );<br />

document.write(sortedArray[0] + "" );<br />

document.write(sortedArray[1] + "" );<br />

document.write(sortedArray[2] + "" );<br />

document.write(sortedArray[3] + "" );<br />

document.write(sortedArray[4] + "" );<br />

<br />

The resulting list is now in reverse order:<br />

Pear<br />

Lemon<br />

Cabbage<br />

Banana<br />

Apple<br />

Making Decisions in <strong>JavaScript</strong><br />

Decision making is what gives programs their apparent intelligence. You can’t write a good program <strong>with</strong>out it,<br />

whether you’re creating a game, checking a password, giving the user a set of choices based on previous decisions he<br />

has made, or something else.<br />

Decisions are based on conditional statements, which are simply statements that evaluate to true or false. This is<br />

where the primitive Boolean data type comes in useful. Loops are the other essential tool of decision making, enabling<br />

you to loop through user input or an array, for example, <strong>and</strong> make decisions accordingly.<br />

36<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!