Java Reference
In-Depth Information
execution at the next iteration, starting with the for or while statement's condition being re-evaluated,
just as if the last line of the loop's code had been reached.
In the break example, it was all or nothing — if even one piece of data was invalid, you broke out of the
loop. It might be better if you tried to convert all the values in degFahren, but if you hit an invalid item
of data in the array, you notify the user and continue with the next item, rather than giving up as our
break statement example does.
if (isNaN(degFahren[loopCounter]))
{
alert(“Data '“ + degFahren[loopCounter] + “' at array index “ +
loopCounter + “ is invalid”);
continue;
}
Just change the break statement to a continue. You will still get a message about the invalid data, but
the third value will also be converted.
Functions
A function is something that performs a particular task. Take a pocket calculator as an example. It per-
forms lots of basic calculations, such as addition and subtraction. However, many also have function keys
that perform more complex operations. For example, some calculators have a button for calculating the
square root of a number, and others even provide statistical functions, such as the calculation of an average.
Most of these functions could be done with the basic mathematical operations of add, subtract, multiply,
and divide, but that might take a lot of steps — it's much simpler for the user if she only needs to press
one button. All she needs to do is provide the data — numbers in this case — and the function key does
the rest.
Functions in JavaScript work a little like the function buttons on a pocket calculator: They encapsu-
late a block of code that performs a certain task. Over the course of the topic so far, you have come
across a number of handy built-in functions that perform a certain task, such as the parseInt()
and parseFloat() functions, which convert strings to numbers, and the isNaN() function, which
tells you whether a particular value can be converted to a number. Some of these functions return data,
such as parseInt(), which returns an integer number; others simply perform an action but return no
data. You'll also notice that some functions can be passed data, whereas others cannot. For example, the
isNaN() function needs to be passed some data, which it checks to see if it is NaN. The data that a func-
tion requires to be passed are known as its parameter(s) .
As you work your way through the topic, you'll be coming across many more useful built-in functions,
but wouldn't it be great to be able to write your own functions? After you've worked out, written, and
debugged a block of code to perform a certain task, it would be nice to be able to call it again and again
when you need it. JavaScript gives us the ability to do just that, and this is what you'll be concentrating
on in this section.
Creating Your Own Functions
Creating and using your own functions is very simple. Figure 3-13 shows an example of a function.
Search WWH ::




Custom Search