-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
35 lines (31 loc) · 1.38 KB
/
app.js
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
(function(){
let aplhabet = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"];
let encryptedMsgArr = [];
function encryptWord() {
let msg = document.getElementById('msg').value;
let msgArray = msg.split(' ');
let encryptKey = document.getElementById('encrypt-key').value;
msgArray.forEach(word => {
let i;
let array = [];
for (i = 0; i < word.length; i++) {
var encryptedLetter;
let letter = word.charAt(i);
let aplhabetNum = aplhabet.indexOf(letter);
let e = aplhabetNum + parseInt(encryptKey);
if ( e > 25) {
e = encryptKey - (aplhabet.length - aplhabetNum);
encryptedLetter = aplhabet[e];
} else {
encryptedLetter = aplhabet[e];
}
array.push(encryptedLetter);
}
let newWord = array.join('');
encryptedMsgArr.push(newWord);
});
let encryptedMsg = encryptedMsgArr.join(' ');
alert(`The encrypted message is "${encryptedMsg}" and the decryption key is ${encryptKey}.`)
}
document.querySelector('button').addEventListener('click', encryptWord);
})()