-
Notifications
You must be signed in to change notification settings - Fork 0
/
14.js
44 lines (35 loc) · 1.29 KB
/
14.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
36
37
38
39
40
41
42
43
44
/**
* Encrypts a string based on capital letters.
* @param {[type]} str the string you want to encrypt
* @param {[type]} shift by how many letters you want to shift your encryption
*/
function encrypt(str, shift) {
const upperCaseStr = str.toUpperCase() // only account for uppercase letters
let encryptedString = []
for (var i = 0; i < upperCaseStr.length; i++) {
let shiftLetters = upperCaseStr.charCodeAt(i) + shift
let newCharValue = String.fromCharCode(shiftLetters)
if(shiftLetters > 90) {
const newIndex = (shiftLetters - 90) - 1
newCharValue = String.fromCharCode(newIndex + 65)
}
encryptedString.push(newCharValue)
}
return encryptedString.join('')
}
function deCrypt(str, shift) {
const upperCaseStr = str.toUpperCase() // only account for uppercase letters
let decryptedString = []
for (var i = 0; i < upperCaseStr.length; i++) {
let shiftLetters = upperCaseStr.charCodeAt(i) - shift
let newCharValue = String.fromCharCode(shiftLetters)
if(shiftLetters < 65) {
const newIndex = (shiftLetters - 65) + 1
newCharValue = String.fromCharCode(newIndex + 90)
}
decryptedString.push(newCharValue)
}
return decryptedString.join('')
}
console.log(deCrypt('DTZWRTRNXHTTQD', 5))
console.log(encrypt("yourmomiscooly", 5))