Lesson 1
Lesson 2
Lesson 3
Lesson 4
Lesson 5
Lesson 6
Lesson 7Lesson 2 - Simple
2-Letter Encoding Program
Note: To use the Javascript programs
in these lessons, you will need to use a
FireFox browser or other non-Internet Explorer browser due to the
"feature" in the IE browser
disallowing Javascript prompts.
This program does the following:
- Prompts you to input a letter. Then outputs the letter (to your
screen). This is done for 8 letters.
- Assigns a variable to your letter. For example, the first
letter is assigned to the variable letter1a.
- Assigns each variable a value of 1 through 26, depending on what
letter it equals where A = 1, B = 2, .....Y = 25, Z = 26. So
if letter1a = "C", then letter1a = 3. If you enter something
other than a letter A-through-Z or you enter nothing at all and hit
the return, the variable is assigned to the value " * "
- Add 2 to each letter variable value using the formulas letter1a
= letter1a +2, letter2b = letter1b +2, letter3c = letter3c +2, etc..
So if letter1a=3, 2 is added and letter1a becomes equal to 5. The
program "adds" 2 to * by calling it *2
- Assigns each new value back to a letter to produce a coded
message. So if letter1a is 5, letter1a is assigned the value
becomes "E". Then outputs the letter. This is done for 8 letters.
Note that if the letter variable was assigned a value of " *2 " or
the new value is something other than a number from 1-to-26, it will
output the value " * ".
To see the code used in this program, select
View, Page Source in your browser's menu.
Problems With This Encoding Scheme!
What happens when you enter the letter "Y" or "Z"? Since "Y"
corresponds to 25 and "Z" corresponds to 26, when these numeric values
are increased by 2, they become 27 and 28 respectively. The program
will then output the value "*" for any "Y" or "Z". For example,
the message "ZIG ZAG " encodes as *KI**CI* as shown below. We
have no way of knowing whether the * stands for a space, a "Y", or a
"Z". We need an improved formula! Go to
Improved Two Letter Shift to
see how this is done.

GO TO LESSON 3
|