-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcaesarEncode.js
More file actions
16 lines (13 loc) · 904 Bytes
/
caesarEncode.js
File metadata and controls
16 lines (13 loc) · 904 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
function encodeStr(s, shift) {
let outArray = [], out, runners
out = s[0].toLowerCase() + String.fromCharCode(s[0].toLowerCase().charCodeAt(0) + shift) + s.replace(/[a-z]/g, char => String.fromCharCode((char.charCodeAt(0) - 97 + shift)%26 + 97)).replace(/[A-Z]/g, char => String.fromCharCode((char.charCodeAt(0) - 65 + shift)%26 + 65))
runners = out.length%4 === 0 ? Math.ceil(out.length/4) : Math.ceil(out.length/5)
for(i=0; i<s.length; i+=runners) {
outArray.push(out.split("").slice(i,i+runners).join(""))
}
return outArray
}
function decode(arr) {
let shift = arr.join("").slice(1,2).charCodeAt(0) - arr.join("").slice(0,1).charCodeAt(0)
return arr.join("").slice(2).replace(/[a-z]/g, char => String.fromCharCode((char.charCodeAt(0) - 97 - shift)%26 + 97)).replace(/[A-Z]/g, char => String.fromCharCode((char.charCodeAt(0) - 65 - shift)%26 + 65))
}