HTML and CSS Reference
In-Depth Information
When you write your own functions, it's more common to put each
operation on a single line, like this:
function evenMe(n) {
if (n % 2 == 1) {
return n + 1;
} else {
return n;
}
}
But JavaScript, like HTML , doesn't care about whitespace; that just
makes it easier for humans to read. It works no matter how many lines
it takes up, but for the console you need to get everything onto one line.
Functions can take more than one argument.
Here's the previous function written in a more conventional style to
make it easier to read. The function rounds the argument n up to the
next multiple of a :
function baseMe(n,a) {
while (n % a != 0) {
n++;
};
return n;
}
Precedence, which you saw in the simple arithmetic examples earlier,
also applies with functions. This is important when you want to pass
the result of one function as an argument to another.
Search WWH ::




Custom Search