Title: B118 Web Programming
1B118 Web Programming
- Session 6 JavaScript Application Examples
- March 8, 2004
2Tonights Agenda
- Quiz 2 Discussion
- JavaScript Examples
- Midterm 1
- Chapters 3 7 quiz next week !!!
- Chapters 10, 11, 14 quiz the week after next !!!
3JavaScript Examples
8.27 A company wants to transmit data over the
telephone but it is concerned that its phones may
be tapped. All of its data are transmitted as
four-digit integers. It has asked you to write a
program that will encrypt its data so that the
data may be transmitted more securely. Your
script should read a four-digit integer entered
by the user in a prompt dialog and encrypt it as
follows Replace each digit by (the sum of that
digit plus7) modulus 10. Then swap the first
digit with the third and swap the second digit
with the fourth. Then output the XHTML text that
displays the encrypted integer. 8.28 Write a
program that inputs an encrypted four-digit
integer (from Exercise 8.27) and decrypts it to
form the original number.
4JavaScript Example 8.27 8.28
- Modulus arithmetic discussed on page 210 of text
- xy produces remainder after x is divided by y.
So 1710 7 2310 3 - How do you separate a four digit value into four
separate values? Hint utilize modulus arithmetic - Whats the pseudocode?
5JavaScript Example Pseudocode
Level A INPUT (four digit number to be
encrypted) EXTRACT (digit1, digit2, digit3,
digit4) FROM (four digit number) OUTPUT (digit3
digit4 digit1 digit2) Level B INPUT (four
digit number to be encrypted) STORE-AS
number digit1 ( number / 1000 7 ) 10 digit2
( (number 1000) / 100 7 ) 10 digit3 (
(number 1000) 100 / 10 7 ) 10 digit4
( number 1000 100 10 7 ) 10 OUTPUT
(digit3 digit4 digit1 digit2)
6JavaScript Example 8.27
// Enter four digit number to be encrypted
inputString window.prompt( "Enter
a four digit number " ) var number
parseInt( inputString ) digit1
parseInt(( number / 1000 7 ) 10)
digit2 parseInt(( number 1000 / 100 7 )
10) digit3 parseInt(( number 1000
100 / 10 7 ) 10) digit4
parseInt(( number 1000 100 10 7 )
10) var encryptedNumber
encryptedNumber "The encrypted number is "
digit3 digit4 digit1 digit2
document.writeln( "ltbr /gt"
encryptedNumber)
7JavaScript
9.11 (The Twelve Days of Christmas Song) Write
a script that uses repetition and switch
structures to print the song The Twelve Days of
Christmas. One switch structure should be used
to print the day (i.e., First, Second, etc.).
A separate switch structure should be used to
print the remainder of each verse.
8Midterm Preparation Exercises
- From this weeks lecture notes (at the bottom)
- 7.18, 7.26
- 8.11, 8.12, 8.16
- Needs material from chapters 10, 11, 12 of
Lagerstrom
9Techniques Youll Need for the Exercises
- window.alert
- Similar to document.writeln
- See Figures 7.3 7.4
- window.prompt (Figure 7.6)
- Algorithms for picking the biggest (or smallest)
item on a list - Cant just look at pairs!
- Calculating total miles/gallon