iEntry 10th Anniversary CSS Snippets JavaScript Snippets

 
 





Math Functions

Math functions can be useful in any application. By using math functions you can make bread crumbs for site navigation, or display random information, like quotes. Like all programming languages, JavaScript has built in math functions.

FunctionDescription
abs(x)Returns the absolute value of x
acos(x)Returns the arccosine of x
asin(x)Returns the arcsine of x
atan(x)Returns the arctangent of x
atan2(y,x)Returns the angel theta of x and y
ceil(x)Returns x rounded up to the nearest integer
cos(x)Returns the cosine of x
exp(x)Returns the value of E to the value of x, where E is the Euler's constant (about 2.7183)
floor(x)Returns x round down to the the nearest integer
log(x)Returns the natural logarithm of x
max(x,y)Returns the value of the highest number
min(x,y)Returns the value of the lowest number
pow(x,y)Returns x to the value of y
random()Returns a value between 0 and 1.
round(x)Rounds x to the lowest integer
sin(x)Returns the sine of x
sqrt(x)Returns the square root of x
tan(x)Returns the tangent of x

Using a few of these function, lets create a script that will randomly display quotes about computers.

<script language="JavaScript" type="text/javascript">
function quote(){
 random_num = Math.random()*10
 quote_num = Math.round(random_num)

 var quotes = new Array;
 quotes[0] = "

Sites need to be able to interact in one single, universal space.

- Tim Berners-Lee " quotes[1] = "

If you don't want to be replaced by a computer, don't act like one.

- Arno Penzias." quotes[2] = "

Computing is not about computers any more. It is about living.

- Nicholas Negroponte" quotes[3] = "

Treat your password like your toothbrush. Don't let anybody else use it, and get a new one every six months.

- Clifford Stoll" quotes[4] = "

Computers are useless. They can only give you answers.

- Pablo Picasso" quotes[5] = "

640K ought to be enough for anybody.

- Bill Gates" quotes[6] = "

There is no reason anyone would want a computer in their home.

- Ken Olson" quotes[7] = "

I do not fear computers. I fear the lack of them.

- Isaac Asimov" quotes[8] = "

Never trust a computer you can't throw out a window.

- Steve Wozniak" quotes[9] = "

Any program is only as good as it is useful.

- Linus Torvalds " quotes[10] = "

I think there is a world market for maybe five computers.

- Thomas Watson" document.getElementById('display_quote').innerHTML = quotes[quote_num] } </script> <div id="display_quote"> ≶script language="JavaScript" type="text/javascript"> quote(); </script> </div>

The first thing the script does is uses the Math.random() function, then multiples that by 10. This will give us a number between 0 and 10, since random gives us a number between 0 and 1. We then round that number to the nearest whole number. This is so that if random_num is 5.285, it will round the number to five. Then we create an array of 10 quotes, and so the quotes look better in our element, we wrap them with paragraph HTML tags. Finally, we display the quotes in an element named display_quote. (If used in a real script, the div tag in the example would be inside the body of the HTML document.) If you refresh the page a few times to get different quotes since the random_num will be different each time.

Random Quote: