18.04.2016 Views

Professional JavaScript For Web Developers

javascript for learners.

javascript for learners.

SHOW MORE
SHOW LESS

Create successful ePaper yourself

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

Even though these methods are defined by ECMA-262, the results are implementation-dependent because<br />

you can calculate each value in many different ways. Consequently, the precision of the results may also<br />

vary from one implementation to another.<br />

The last method of the Math object is random(). This method returns a random number between the 0<br />

and 1, not including 0 and 1. This is a favorite tool of <strong>Web</strong> sites that are trying to display random quotes<br />

or random facts upon entry. You can use random() to select numbers within a certain range by using the<br />

following formula:<br />

number = Math.floor(Math.random() * total_number_of_choices + first_possible_value)<br />

The floor() method is used here because random() always returns a decimal value, meaning that multiplying<br />

it by a number and adding another still yields a decimal value. Most of the time, you want to<br />

select a random integer. Because of that, the floor() method is needed. So, if you wanted to select a<br />

number between 1 and 10, the code looks like this:<br />

var iNum = Math.floor(Math.random() * 10 + 1);<br />

You see 10 possible values (1 through 10) with the first possible value being 1. If you want to select a<br />

number between 2 and 10, then the code looks like this:<br />

var iNum = Math.floor(Math.random() * 9 + 2);<br />

There are only nine numbers when counting from 2 to 10, so the total number of choices is 9 with the<br />

first possible value being 2. Many times, it’s just easier to use a function that handles the calculation of<br />

the total number of choices and the first possible value:<br />

function selectFrom(iFirstValue, iLastValue) {<br />

var iChoices = iLastValue – iFirstValue + 1;<br />

return Math.floor(Math.random() * iChoices + iFirstValue);<br />

}<br />

//select from between 2 and 10<br />

var iNum = selectFrom(2, 10);<br />

Using the function, it’s easy to select a random item from an Array:<br />

var aColors = [“red”, “green”, “blue”, “yellow”, “black”, “purple”, “brown”];<br />

var sColor = aColors[selectFrom(0, aColors.length-1)];<br />

Here, the second parameter to selectFrom() is the length of the array minus 1, which (as you remember)<br />

is the last position in an array.<br />

Host objects<br />

Object Basics<br />

Any object that is not native is considered to be a host object, which is defined as an object provided by<br />

the host environment of an ECMAScript implementation. All BOM and DOM objects are considered to<br />

be host objects and are discussed later in the book.<br />

87

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

Saved successfully!

Ooh no, something went wrong!