Lecture 25
Comments on Exam 2
7. What is the output of the following?
var Grade = 62;
if ( Grade >= 60 )
document.writeln("You Passed<BR>");
else
document.writeln("Low Grade<BR>");
document.writeln("Study Harder<BR>");
The else clause ends with ("Low Grade<BR>");
The statementdocument.writeln("Study
Harder<BR>"); is a separate action.
To get both document.writeln
statements as part of the else clause:
var Grade = 62;
if ( Grade >= 60 )
document.writeln("You Passed<BR>");
else
{
document.writeln("Low Grade<BR>");
document.writeln("Study Harder<BR>");
}
10. Rewrite the following for loop as an equivalent while loop
var i;
for (i = 0; i <= 5; i = i + 1)
document.writeln("Middlebury
Rocks!");
var i;
i = 0; ( initialize i)
while ( i <= 5)
{ document.writeln("Middlebury Rocks!");
i = i + 1 } (
increment i)
i must have a value for the first termination test.
We must also change i
inside the body of the loop
11. Given the following declarations, write a loop which determines and reports the product of the numbers in the array.
var i, A = [2, 1.414, 7.8, 1776, 3.14156, 8, 9, 11], Product;
Product = 1;
for ( i = 0; i < A.length ; i = i + 1)
Product = Product * A[i];
document.writeln("The
product is " + Product);
i | Product |
i = 0 | 1 * A[0] = 1 * 2 = 2 |
i= 1 | (1*2) * A[1] = 1* 2 * 1.414 |
i= 2 | (1*2*1.414)*A[2] = 1* 2 * 1.414 * 7.8 |
12. With the declarations below what do the following JavaScript statements produce?
var W = "WORDS";
1. | function RemoveSomething (A) |
2. | { var X = ""; |
3. | for (i = 0; i <= A.length-2; i = i+1) |
4. | X = X + A.charAt(i); |
5. | W = X; |
6. | document.writeln(W + "<BR>"); |
7. | } |
8. | |
9. | RemoveSomething(W); |
10. | RemoveSomething(W); |
Action begins with Line 9:
RemoveSomething(W);
Current Value of W is "WORDS"
The function is called:
A copy of W is put into A
A.length is
5
A[0] | A[1] | A[2] | A[3] | A[4] | X | |
W | 0 | R | D | S |
Line 3: for (i = 0; i <= A.length-2; i = i+1)
X = X + A.charAt(i);
i will take
on values 0, 1, 2, 3
step i | Action |
i = 0 | X
= X + A.charAt(0);
X = "" + "W" = "W" |
i = 1 | X
= X + A.charAt(1);
X = "W" + "O" = "WO" |
i = 2 | X
= X + A.charAt(2);
X = "WO" + "R" = "WOR" |
i = 3 | X
= X + A.charAt(3);
X = "WOR" + "D" = "WORD" |
The Loop is now Terminated
with X = "WORD"
1. | function RemoveSomething (A) |
2. | { var X = ""; |
3. | for (i = 0; i <= A.length-2; i = i+1) |
4. | X = X + A.charAt(i); |
5. | W = X; |
6. | document.writeln(W + "<BR>"); |
7. | } |
8. | |
9. | RemoveSomething(W); |
10. | RemoveSomething(W); |
Now we're ready to do Step 5:
The result is W = "WORD"
Finally, Step 6 displays WORD on the screen.
Thus the effect of calling the function RemoveSomething is to chop off the last character of W and print the remaining shortened string followed by a line break.
We have completed Step 9, the first call of the function.
The result of Step 10 is to chop off the D in
WORD so that W becomes WOR and then to print WOR
Hence the output is:
WORD
WOR
13. With the declarations
of Problem 12, what is the outcome of the following?
1. | function Twist(A) |
2. | { L = A.length; |
3. | document.write( A.charAt(L-1) ); |
4 | if (L != 1) |
5. | { L = A.length; |
6. | RemoveSomething(W); |
7. | Twist(W); |
8. | } |
9. | } |
10. | Twist(W); |
10.
Twist(W)
= Twist("WORDS")
Screen | Action | ||
1. | function Twist("WORDS") | ||
2. | { L = 5 | ||
3. | document.write( A.charAt(4) ); | S | |
4 | . if (L != 1) | ||
5. | { | ||
6. | RemoveSomething(W); | SWORD | W=WORD |
7. | Twist(W); | ||
8. | } | ||
9. | } |
7.
Twist(W)
= Twist("WORD")
Screen | Action | ||
1. | function Twist("WORD") | ||
2. | { L = 4 | ||
3. | document.write( A.charAt(3) ); | SWORD
D |
|
4 | . if (L != 1) | ||
5. | { | ||
6. | RemoveSomething(W); | SWORD
DWOR |
W=WOR |
7. | Twist(W); | ||
8. | } | ||
9. | } |
Each call of Twist writes
the last character of the current value of W, chops off that character
fom W, and then writes the remainder of W on the same line and finally
causes a line break:
Thus the output is
SWORD
DWOR
RWO
OW
W
14. What is the output of the following JavaScript program if X and Y are positive integers?
<HEAD>
<SCRIPT LANGUAGE = "JavaScript" >
function Mystery(A,B)
{
if ( B = = 1) return A;
else return A + Mystery(A, B-1);
}
X = parseInt(window.prompt("Enter a positive integer", "") );
Y = parseInt(window.prompt("Enter a positive integer", "") );
document.writeln( Mystery(X,Y) ) ;
Y | What is written | |
1 | Mystery(X,1) | X |
2 | Mystery(X,2) | X + Mystery(X,1) = X + X = 2X |
3 | Mystery(X,3) | X + Mystery(X,2) = X + 2X = 3X |
4 | Mystery(X,4) | X + Mystery(X,3) = X + 3X = 4X |
Mystery(X,Y) = X * Y
Where have we seen this function before:
{
if ( B = = 1) return A;
else return A * Mystery(A, B-1);
}
function Power(Base, Exponent)
{
if ( Exponent = = 1) return Base;
else return Base * Power(Base, Exponent-1);
}
Due: Friday, May 11
JavaScript should be fully
annotated. Submit program and printout of sufficiently ample runs of the
program to show that it functions properly. You must also provide an electronic
version of the program or a URL for locating it.
From Deitel, Deitel & Nieto
11.32/33 | Guess the Number |
11.39 | Enhanced CAI |
11.43 | Craps with Wagers |
12.15/16 | More Shooting Craps |
12.21 | Turtle Graphics |
12.22 | Knight's Tour |
12.27 | Sieve of Eratosthenes |
12.28 | Bucket Sort |
12.37 | Quicksort |
12.38-40 | Mazes |
12.41 | Tortoise and the Hare |
CX 103 Computer Dating Questionnaire
Name
Answer Each of the following Questions with a "YES" or a "NO"
You may check the appropriate box:
Question | Yes | No |
Do you prefer raw carrots to cooked carrots? | ||
Do you prefer black jelly beans to other colors? | ||
Do you like Burger King more than McDonald's? | ||
Do you prefer to squeeze toothpaste from the bottom of the tube? | ||
Do you like the morning more than the evening? | ||
Do you prefer cats to dogs? | ||
Do you prefer the Red Sox to the Yankees? | ||
Do you prefer jazz to opera? | ||
Do you prefer pickup trucks to SUV's? | ||
Do you enjoy sleeping with the window open? |
Arrays and Computer
Dating Services
#1 | #2 | #3 | #4 | #5 | #6 | #7 | #8 | #9 | #10 | |
Andrew | Yes | No | No | Yes | Yes | Yes | Yes | No | No | No |
Annalise | Yes | Yes | Yes | No | No | Yes | No | No | Yes | Yes |
Beth | Yes | Yes | No | No | Yes | No | Yes | Yes | Yes | Yes |
Bob | No | Yes | Yes | Yes | No | No | No | Yes | No | No |
Brendan | Yes | No | Yes | Yes | Yes | No | No | Yes | Yes | Yes |
Casey | Yes | No | Yes | Yes | No | Yes | Yes | No | Yes | Yes |
Charles M | Yes | No | No | Yes | Yes | No | Yes | Yes | No | Yes |
Charles R | No | Yes | No | Yes | Yes | No | No | Yes | No | No |
Colin | Yes | Yes | Yes | No | No | No | Yes | Yes | Yes | Yes |
David M | Yes | No | Yes | Yes | No | Yes | No | Yes | Yes | No |
David R | No | No | No | No | No | Yes | Yes | Yes | Yes | No |
David S | Yes | No | No | Yes | No | Yes | No | No | No | Yes |
Eliza | Yes | Yes | No | Yes | Yes | No | No | Yes | Yes | Yes |
John | No | Yes | No | Yes | Yes | No | Yes | No | No | Yes |
Jordan | Yes | Yes | No | Yes | Yes | No | Yes | No | Yes | No |
Kate | Yes | No | No | No | No | No | Yes | Yes | No | Yes |
Keenen | Yes | Yes | No | No | No | No | No | Yes | No | Yes |
Liz | No | Yes | No | Yes | Yes | No | No | No | Yes | Yes |
Louisa | Yes | No | No | Yes | No | No | Yes | Yes | No | No |
Matt | No | Yes | No | No | Yes | Yes | No | Yes | Yes | No |
Megan | Yes | No | No | Yes | Yes | Yes | Yes | No | Yes | No |
Missy | No | No | No | Yes | No | No | No | Yes | No | No |
Nathaniel | Yes | No | No | No | Yes | No | No | Yes | No | Yes |
Rada | No | No | Yes | Yes | Yes | No | Yes | Yes | Yes | Yes |
Rebecca | No | Yes | No | Yes | No | No | No | Yes | No | Yes |
Ryan B | Yes | No | Yes | Yes | Yes | Yes | No | No | Yes | No |
Ryan P | Yes | Yes | No | No | Yes | No | No | No | Yes | Yes |
Ted | Yes | No | Yes | Yes | No | Yes | Yes | Yes | Yes | Yes |
Tejas | Yes | No | Yes | Yes | No | No | Yes | Yes | Yes | No |
Trevor | No | Yes | No | Yes | No | Yes | Yes | No | No | Yes |
Tyler | No | Yes | No | No | No | No | Yes | Yes | Yes | No |
Veljko | No | Yes | Yes | Yes | No | No | Yes | No | No | Yes |
Students is an array with 33 elements with elements 1 through 32 corresponding to the individual students in the class. The element in cell 0 is the instructor, but his answers to the questions are not known.
Each element Student[i] is itself an array with 12 cells. In cell 0 is the student's name. In cells 1 - 10 are the "Yes" or "No" answers to each question.
Cell 11 is reserved for
saving the number of matches with some other prescribed student
Rebecca | No | Yes | No | Yes | No | No | No | Yes | No | Yes | |
David S | Yes | No | No | Yes | No | Yes | No | No | No | Yes | 6 |
Rebecca | No | Yes | No | Yes | No | No | No | Yes | No | Yes | |
Tyler | No | Yes | No | No | No | No | Yes | Yes | Yes | No | 6 |
Rebecca | No | Yes | No | Yes | No | No | No | Yes | No | Yes | |
Veljko | No | Yes | Yes | Yes | No | No | Yes | No | No | Yes | 7 |
ENCRYPTION ASSIGNMENT
Original:
Break message into pairs of consecutive characters.
Replace each pair by 4 digit number made up of their ASCII values
Add 7 % 10 to each digit.
Reverse the digits.
1. | <HTML> |
2. | <HEAD> |
3. | <TITLE>Stage One of Encryption</TITLE> |
4. | <SCRIPT LANGUAGE = "Javascript"> |
5. | |
6. | var W, i, S; |
7. | |
8. | |
9. | function Begin() |
10. | { var i, A,B,S, T, Number, Length, M, First, Second, Third, Fourth; |
11. | W = document.EncryptForm.Phrase.value; |
12. | |
13. | Length = W.length; |
14. | |
15. | S = "It has " + Length + " characters."; |
16. | Append = "That
is an odd number. I will append an X to the end and
proceed with the encryption." |
17. | |
18. | |
19. | |
20. | if (W.length % 2 == 1) |
21. | { S = S + Append; |
22. | W = W + "X"; |
23. | Length = Length + 1 } |
24. | |
25. | var Originals = new Array(Length/2); |
26. | |
27. | document.EncryptForm.Comment.value = S; |
28. | |
29. | T = "Its original numerical representation is "; |
30. | document.EncryptForm.Digits.value = T; |
31. | |
32. | for (i = 0; i < Length ; i = i + 2) |
33. | { A = W.charCodeAt(i) ; |
34. | B = W.charCodeAt(i+1) ; |
35. | Number = 100*A + B; |
36. | Originals[i/2] = Number; |
37. | } |
38. | |
39. | document.EncryptForm.plain.value = Originals.join(" ");; |
40. | |
41. | var Scrambleds = new Array (Length/2); |
42. | var Digits = new Array(4) |
43. | |
44. | function power(k) |
45. | { var i, N; |
46. | N = 1; |
47. | for (i = 1; i <= k; i = i+ 1) N = N * 10; |
48. | return N; |
49. | } |
50. | |
51. | for (i = 0; i < Length/2; i = i + 1) |
52. | { |
53. | M = Originals[i]; |
54. | for (j = 0; j <= 3; j = j+ 1) |
55. | { Digits[j] = Math.floor(M/power(j)); |
56. | Digits[j] = ( Digits[j] + 7) % 10; } |
57. | M = 0; |
58. | for (j = 0; j <= 3; j = j+ 1) |
59. | M = M + Digits[j] * power(3-j); |
60. | |
61. | |
62. | Scrambleds[i] = M |
63. | } |
64. | |
65. | document.EncryptForm.Scrambled.value = Scrambleds.join(" "); |
66. | |
67. | |
68. | } |
69. | |
70. | |
71. | </SCRIPT> |
72. | </HEAD> |
73. | <BODY BGCOLOR = "white"> |
74. | <FORM NAME = "EncryptForm"> |
75. | Enter a word or phrase <INPUT NAME = "Phrase" SIZE = 80 TYPE = "text"> |
76. | <BR> |
77. | <INPUT TYPE = "button" VALUE = "Click to Encrypt" SIZE = "20" |
78. | ONCLICK = "Begin()" > |
79. | <BR> |
80. | <TEXTAREA
NAME = "Comment" ROWS =4 COLS = 50 TYPE =
"text"> </TEXTAREA> |
81. | <BR> |
82. | <INPUT NAME = "Digits" SIZE = 50 TYPE = "text"> |
83. | <BR> |
84. | <TEXTAREA NAME = "plain" ROWS = 5 COLS = 50 TYPE = "text"> </TEXTAREA> |
85. | <BR> |
86. | <H2>The encrypted message is: |
87. | <BR> |
88. | <TEXTAREA
NAME = "Scrambled" ROWS = 5 COLS = 50
TYPE = "text"> |
89. | </TEXTAREA> |
90. | </FORM> |
91. | </BODY> |
92. | </HTML> |
How secure is this method?
How can we make it more secure?
(1) Use a different number other than 7 to add mod 10. Keep that digit secret.
(2) Try some other rearrangment of the digits. (also secret)
How many possibilities are there?
( 10 numbers ) * (4! = 24 rearrangements) = 240
Could we "crack" a secret
message by brute force?
(3) Use a different number to add to each digit
Example: Add 2 % 10 to First Digit
Add 9 % 10 to Second Digit
Add 5 % 10 to Third Digit
Add 7 % 10 to Fourth Digit
How many possibilites are there now?
10 * 10 * 10 * 10 * 4!
= 240, 000
(4)
Use triples of characters to make up six digit numbers.
How many possibilites are there now?
( 10 * 10 * 10 * 10 *
10 * 10) * 6! = 720,000,000
For now, let's continue to add 7 % 10 to each digit and reverse the digits when we are done, but we'll work with 6 digit numbers instead of 4 digit numbers:
Example:
MIDDLEBURY_COLLEGE
Triple | ASCII | + 7 % 10 | Reverse |
MID | 777368 | 444035 | 530444 |
DLE | 687669 | 354336 | 633453 |
BUR | 668582 | 952533 | |
Y_C | 893267 | 439065 | |
OLL | 797676 | 343464 | |
EGE | 697160 | 638463 | |
M | I | D | D | L | E | B | U | R | Y | C | O | L | L | E | G | E | |
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 |
77 | 73 | 68 | 68 | 76 | 69 | 66 | 85 | 82 | 89 | 32 | 67 | 79 | 76 | 76 | 69 | 71 | 69 |
Programming Issues:
(1)
W is "plaintext" character string
Length of W is W.length
If W.length is not a multiple of 3, we need to pad on extra X's at the end.
How many X's ?
W.length% 3 | Number of X's |
0 | 0 |
1 | 2 |
2 | 1 |
Extensions: If we plan to break up message into blocks of k letters, then length must be a multiple of k.
Example: k = 6
W.length % 6 | Number of X's | |
0 | 0 | |
1 | 5 | 6 - 1 |
2 | 4 | 6 - 2 |
3 | 3 | 6 - 3 |
4 | 2 | 6 - 4 |
5 | 1 | 6 - 5 |
Issue (2): Getting the
six digit number
32. | for (i = 0; i < Length ; i = i + 2) |
33. | { A = W.charCodeAt(i) ; |
34. | B = W.charCodeAt(i+1) ; |
35. | Number = 100*A + B; |
36. | Originals[i/2] = Number; |
for (i = 0; i < Length ; i = i + 3)
{ A = W.charCodeAt(i) ;
B = W.charCodeAt(i+1);
C = W.charCodeAt(i+2);
Number = 10000*A + 100*B+ C;
Originals[i/3] = Number; }
Questions:
What if we use blocks of 6 consecutive characters?
k consecutive characters where k is specified by user when program runs?
Issue (3) Getting the
individual digits:
51. | for (i = 0; i < Length/2; i = i + 1) |
52. | { |
53. | M = Originals[i]; |
54. | for (j = 0; j <= 3; j = j+ 1) |
55. | { Digits[j] = Math.floor(M/power(j)); |
56. | Digits[j] = ( Digits[j] + 7) % 10; } |
57. | M = 0; |
58. | for (j = 0; j <= 3; j = j+ 1) |
59. | M = M + Digits[j] * power(3-j); |
Issue (4) reporting the
results:
41. | var Scrambleds = new Array (Length/2); |
62. | Scrambleds[i] = M |
63. | } |
64. | |
65. | document.EncryptForm.Scrambled.value = Scrambleds.join(" "); |
86. | <H2>The encrypted message is: |
87. | <BR> |
88. | <TEXTAREA
NAME = "Scrambled" ROWS = 5 COLS = 50
TYPE = "text"> |