Last active
February 14, 2023 12:46
-
-
Save dcortesnet/48b8af44fa8f408adb35e3d0ebee1b35 to your computer and use it in GitHub Desktop.
Javascript cifrado de cesar en
This file contains hidden or 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
| 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