The JavaScript Diaries: Part 14
Methods
| Name | Description |
| abs() | Returns the absolute value of a number |
| acos() | Returns the arccosine (in radians) of a number |
| asin() | Returns the arcsine (in radians) of a number |
| atan() | Returns the arctangent (in radians) of a number |
| atan2() | Returns the arctangent of the quotient of its arguments |
| ceil() | Returns the smallest integer greater than or equal to a number |
| cos() | Returns the cosine of a number |
| exp() | Returns Enumber, where number is the argument, and E is Euler's constant, the base of the natural logarithms |
| floor() | Returns the largest integer less than or equal to a number |
| log() | Returns the natural logarithm (base E) of a number |
| max() | Returns the greater of two numbers |
| min() | Returns the lesser of two numbers |
| pow() | Returns base to the exponent power, that is, base exponent |
| random() | Returns a pseudo-random number between 0 and 1 |
| round() | Returns the value of a number rounded to the nearest integer |
| sin() | Returns the sine of a number |
| sqrt() | Returns the square root of a number |
| tan() | Returns the tangent of a number |
Usage in Scripts
Let's see what we can do with some of these methods. (By now you can probably figure out these scripts without an explanation, so I won't go into greater detail. Instead, we'll focus on the methods themselves. Still, if a script begins to become complex, I'll stop and explain it. If you need further help, you can always e-mail me.)
Finding the Square Root
An obvious use is to find the square root of a number. Here's a basic script for doing the calculation:
This then would be called by the following, placed in the body of the document:
Rounding Numbers
Another calculation we could perform would be to round off a number. This script uses the code we wrote previously to find the circumfrence of a circle.
You could also use the round() method to set the number of decimal places:
The formula for rounding a number to x decimal points is:
|
Keep in mind that the format is as follows:
|






Loading Comments...