Useful JavaScript Functions by Jethro
-------------------------------------

Index:

o Introduction
o The Funcions
o IsNumber(number)
o CrazyText(string)
o IsNegative(number)
o reverse(string)
o Encrypt(string)
o GetChar(string, character_position)
o BodyObject()
o Conclusion


Introduction
------------

Here's a collection of some useful JavaScript functions I have written.
I have tried making them as short and efficient as possible to help
anybody new to the language learn some new techniques of JavaScript
programming.



Functions
---------

Here's the list of functions and included in the actual script is an
example of them in action.



IsNumber(number)
================

This function checks if an argument is a number.


**** isnumber.html ****
<script language="JavaScript">
function IsNumber(number) {
if ((number >= 0)||(number < 0)) return true;
else return false;
}

var age = prompt("Please Enter Your Age","");
if (IsNumber(age)) alert("Thank You!");
else alert("That's not your *real* age!");
</script>
***********************



CrazyText(string)
=================

This function muddles up a string and puts letters here there and
everywhere. Try entering a string like ``1234567890`` to see how it
really muddles them up.

**** crazytext.html ****
<script language=javascript>

function getNewName(name) {
var letters = new Array(name.length);
var weird = "";
for (i=1; i<=name.length; i++)
letters[i] = name.substring(i,i-1);
for (i=1; i<=letters.length-3; i++)
weird += letters[i]+letters[i+2]
return weird;
}

var yourname = prompt("What is your name?","");
var weirdname = getNewName(yourname);
alert("Regular: "+yourname+"\nWeird: "+weirdname);

</script>
************************



IsNegative(number)
==================

This checks whether a number is positive or negative. Should be used
with the IsNumber() function because without it, strings will be seen
as positive (non-negative) numbers.

**** isnegative.html ****
<script language=javascript>

function IsNegative(number) {
return number < 0 ? true : false;
}

var yourage = prompt("Please enter your age","");
if (IsNegative(yourage)) alert("Your age is a negative number?!");
else alert("Thank you!");

</script>
*************************



reverse(string)
===============

This functions accepts a string and returns the backwards version of
it. There are too many reasons why you would need to use this
function.... I'm sure

**** reverse.html ****
<script language=javascript>

function reverse(name) {
var length = name.length;
var letters = new Array(length);
var letters2 = new Array(length);
var backword = "";
for (i=0; i<=length-1; i++) {
letters[i] = name.substring(i, i+1);
}
f = 0
for (j=i-1; j>=0; j--) {
letters2[f] = letters[j];
f++;
}
for (h=0; h<=letters2.length-1; h++) {
backword +=letters2[h];
}
return backword;
}

var yourname = prompt("What is your name?","");
var thename = reverse(yourname);
alert(thename);
</script>
*********************



encrypt(string)
===============

This is a script to encrypt a string. Well, when I say ``encrypt``, I
mean a simple letter subsitution. At the moment it's only set to
convert vowels to numbers (1337-style) but you can avail of ASCII to
the fullest if you want


***** encrypt.html *****
<script language=javascript>

function encrypt(name) {
var alpha = new Array("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z");

var crypt = new Array("4","B","C","D","3","F","G","H","1","J","K","L","M","N","0","P","Q","R","S","T","U","V","W","X","Y","Z");

var newword = " ";

for (i=0; i<=name.length; i++) {
var char = name.substring(i, i+1);
for (j=0; j<=alpha.length; j++) {
if (char==alpha[j]) newword += crypt[j];
}
}
return newword;
}

var name = prompt("What is your name?","");
alert(encrypt(name));
</script>
************************



GetChar(string, char_pos)
=========================

This function returns the letter which is located a certain distance
away from the start of the string. For instance: getchar("hello", 4)
would return 'o'...etc.

***** getchar.html *****
<script language=javascript>
/* Note:
The first character position in a string is
located at 0, not 1. */

function getchar(string, char_pos) {
return string.substring(char_pos, char_pos+1);
}

alert("The first letter in 'Jethro' is: "+getchar("Jethro", 0));
alert("The second letter in 'Jethro' is: "+getchar("Jethro", 1));
</script>
************************



BodyObj()
=========

This object has properties which might be useful for you to use at some
time or another. Some mightn't work for you depending on your browser
and such, but you should get an understand of object-orientated
programming in JavaScript, if nothing else.

***** bodyobj.html *****
<script language=javascript>
function bodyobj() {

this.bg = document.bgcolor;
this.fg = document.fgcolor;
this.alink = document.alink;
this.vlink = document.vlink;
this.appname = navigator.appName;
this.href = location.href;
this.search = location.search;
this.port = location.port;
this.mod = document.lastModified;

}

var mypage = new bodyobj();

alert("Your browser: "+mypage.appname);
alert("Page Location: "+mypage.href);
alert("Webserver Port: "+mypage.port);
alert("Last Modified: "+this.mod);
</script>
***********************



Conclusion
----------

You might find use for some of these JavaScript functions in your
website or web applications. You can modify them to suit your needs.

If you have any questions/queries/comments, contact Jethro at
jethrojones@gmx.net.

Have fun JavaScripting,
Jethro