-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcipher.js
More file actions
65 lines (44 loc) · 1.39 KB
/
cipher.js
File metadata and controls
65 lines (44 loc) · 1.39 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
const cipher = {
// ...METER FUNCIONES ENCODE y DECODE//
//CIFRAR//
encode(offset, string) {
if (offset === undefined || offset === 0 || offset === null) {
throw new TypeError("Número secreto NO válido 💔");
}
if (string === undefined || string.length === 0 || string === null) {
alert("No has escrito un mensaje 💔");
}
let textResult = "";
for (let i = 0; i < string.length; i++) {
let ascii = string[i].charCodeAt();
if (ascii !== 32) {
const newAscci = ascii + offset;
ascii = ((newAscci - 65) % 26 + 65);
}
textResult += String.fromCharCode(ascii);
}
return textResult;
},
// DECIFRAR //
decode(offset, string) {
if (offset === undefined || offset === 0 || offset === null) {
throw new TypeError("Número secreto NO válido 💔");
}
if (string === undefined || string.length === 0 || string === null) {
alert("No has escrito un mensaje 💔");
}
let textResult = "";
for (let i = 0; i < string.length; i++) {
let ascii = string[i].charCodeAt();
if (ascii !== 32) {
ascii = ascii - (offset % 26);
if (ascii < 65) {
ascii += 26;
}
}
textResult += String.fromCharCode(ascii);
}
return textResult;
}
};
export default cipher;