[an error occurred while processing this directive]
javascript tutorial graphic


Webteacher Software now offers

function encode(text) { Ref="0123456789abcdefghijklmnopqrstuvwxyz.-~ABCDEFGHIJKLMNOPQRSTUVWXYZ"
Result=""
for (Count=0; Count<text.length; Count++) {

Char=text.substring (Count, Count+1);
Num=Ref.indexOf (Char);
EncodeChar=Ref.substring(Num+1, Num+2)
Result += EncodeChar
} document.form1.result.value=Result
}


First we defined a variable to contain every letter and number

Ref="0123456789abcdefghijklmnopqrstuvwxyz.-~ABCDEFGHIJKLMNOPQRSTUVWXYZ"
is just a variable named Ref which contains a very long sting of text, every possible letter or number. The purpose of the Ref variable is to convert letters to numbers based on their position withi n Ref. It can also be used to convert numbers back to text by locating the letter at a certain position inside Ref.

For example, in the English alphabet, I could easily create a code such that A=1 and B=2 because A is the first letter, and B is the second. Ref allows me to create the same code, but it includes all possible leters and number s. The letter 'a' returns a value of 10 because it is the 10th letter in from the left. (Actually it's the eleventh, but JavaScript begins counting with zero just to keep us on our toes).

Later we will use the command Num=Ref.indexOf(Char) to locate the letter Char inside Ref and assign its position (how many places in) to the variable Num We'll discuss the indexOf property in more detail later.


The second line of the function, Result="" is sometimes called "priming" a variable. This statement establishes that a variable called Result does exist, and that it can contain text, but does not conatin any right now. Later, I will tack letters on to the end of Result, so I need to have it in place.

Tell me about the loops

Home | WebData - Web Database Software | Javascript | CGI | Consulting | Map Builder | Contact Us | The Press Room