-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
50 lines (40 loc) · 1.47 KB
/
script.js
File metadata and controls
50 lines (40 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
function code() {
/* collect the message from the user with a prompt,
save as a variable */
var message=prompt("What is your message?");
/* collect the code fromt the user (any number) with a prompt,
save as a variable*/
var key=prompt("What is the code key?");
/* create a string variable to store the code*/
var code="";
/* use a for loop to convert each character in the
message string into the ASCII code number*/
for (i=0; i<message.length; i++){
/* Convert key from string to number so that it can
be added properly to the ASCII code*/
code+=(message.charCodeAt(i)+Number(key))+" ";
}
/* send an alert to the user the code*/
alert(code);
}
function trans(){
/* collect the code from the user with a prompt,
save as a variable */
var code=prompt("What is the code?");
/* collect the code fromt the user (the number used to make the code)
with a prompt, save as a variable*/
var key=prompt("What is the code key?");
/* convert the code (a string variable)
at each space character*/
var array= code.split(" ");
/* create a new string variable to store
the message */
var message="";
/* use a for loop to turn each number from the
code array back into a letter*/
for (i=0; i<array.length; i++){
message+= String.fromCharCode(array[i]-key);
}
/* alert the translated message to the user*/
alert(message);
}