Created
May 30, 2021 04:04
-
-
Save CarlaTeo/2548551a3bf67800e9786ebf6df42159 to your computer and use it in GitHub Desktop.
[JS] Rotational cipher
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
LOWERCASE_LETTERS = "abcdefghijklmnopqrstuvwxyz"; | |
function isNumber(char) { | |
return !isNaN(Number(char)); | |
} | |
function isLowerCase(char) { | |
return char === char.toLowerCase(); | |
} | |
function rotationalCipher(input, rotationFactor) { | |
let result = ""; | |
for(const char of input) { | |
const charIndex = LOWERCASE_LETTERS.indexOf(char.toLowerCase()); | |
if(charIndex > -1) { | |
const cipheredChar = LOWERCASE_LETTERS[((charIndex + rotationFactor) % LOWERCASE_LETTERS.length)]; | |
if(isLowerCase(char)) result += cipheredChar; | |
else result += cipheredChar.toUpperCase(); | |
} | |
else if(isNumber(char)) { | |
result += String((Number(char) + rotationFactor) % 10); | |
} | |
else result += char; | |
} | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment