Skip to content

Instantly share code, notes, and snippets.

@CarlaTeo
Created May 30, 2021 04:04
Show Gist options
  • Save CarlaTeo/2548551a3bf67800e9786ebf6df42159 to your computer and use it in GitHub Desktop.
Save CarlaTeo/2548551a3bf67800e9786ebf6df42159 to your computer and use it in GitHub Desktop.
[JS] Rotational cipher
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