Skip to content

Instantly share code, notes, and snippets.

@dcortesnet
Last active February 14, 2023 12:46
Show Gist options
  • Select an option

  • Save dcortesnet/48b8af44fa8f408adb35e3d0ebee1b35 to your computer and use it in GitHub Desktop.

Select an option

Save dcortesnet/48b8af44fa8f408adb35e3d0ebee1b35 to your computer and use it in GitHub Desktop.
Javascript cifrado de cesar en
const abcValues = [
"A",
"B",
"C",
"D",
"E",
"F",
"G",
"H",
"I",
"J",
"K",
"L",
"M",
"N",
"O",
"P",
"Q",
"R",
"S",
"T",
"U",
"V",
"W",
"X",
"Y",
"Z"
]
function isAlpha(str) {
return /^[a-zA-Z()]+$/.test(str);
}
function movePosition(position) {
position += 13;
if (position >= 26) {
position -= 26;
}
return position;
}
function rot13(str) {
const arrStr = str.split('')
const arrCipher = arrStr.map(char => {
if (isAlpha(char)) {
let position = abcValues.indexOf(char);
position = movePosition(position);
return abcValues[position];
}
return char;
});
return arrCipher.join("");
}
console.log(rot13("SERR PBQR PNZC"));
/** * rot13("SERR PBQR PNZC") debe decodificarse en la cadena FREE CODE CAMP */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment