Skip to content

Instantly share code, notes, and snippets.

@pchrysa
Last active October 11, 2016 10:38
Show Gist options
  • Save pchrysa/0c8b48b7fc324f2e420783b634f35915 to your computer and use it in GitHub Desktop.
Save pchrysa/0c8b48b7fc324f2e420783b634f35915 to your computer and use it in GitHub Desktop.
Playing with passphrases
function playPass(s, n) {
let finalStr = '';
for (let i in s) {
let letter = s[i];
if (isLetter(letter)) {
finalStr += upperOrLower(nextLetter(letter, n), i);
} else {
if (isNumeric(letter)) {
finalStr += "" + nineComplement(letter);
} else {
finalStr += letter;
}
}
}
return reverse(finalStr);
}
function nineComplement(digit) {
return 9 - digit;
}
function isLetter(c) {
return c.toLowerCase() != c.toUpperCase();
}
function nextLetter(char, num) {
let alphabet = "abcdefghijklmnopqrstuvwxyz",
isUpper = char === char.toUpperCase() ? true : false;
char = char.toLowerCase();
let newIndex = alphabet.indexOf(char) + num;
if(newIndex < alphabet.length) {
char = alphabet[newIndex];
} else {
let shiftedIndex = -(alphabet.length - newIndex);
char = alphabet[shiftedIndex];
}
if (isUpper) {
char = char.toUpperCase();
}
return char;
}
function isNumeric(n) {
return !isNaN(parseInt(n));
}
function upperOrLower(c, position) {
if (position%2 == 0) {
return c.toUpperCase();
} else {
return c.toLowerCase();
}
}
function reverse(s) {
return Array.from(s).reverse().join('')
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment