function rot13(message){
console.log(message)
const alphabet = 'abcdefghijklmnopqrstuvwxyz'.split('');
const alphabetMayo = 'abcdefghijklmnopqrstuvwxyz'.toUpperCase().split('');
let rot = []
let fin = []
const arrayMe = message.toLowerCase().split('')
// console.log(message)
for (let i = 0; i < arrayMe.length; i++) {
for (let l = 0; l < alphabet.length; l++ ) {
var a = arrayMe[i]
var b = alphabet[l]
var r = alphabet[l + 13];
if (a === b) {
if (l > 13) {
var e = alphabet[l + 13 - 26];
rot.push(e)
} else {
rot.push(r)
}
}
}
}
for(let g = 0; g < arrayMe.length; g++) {
if ((/[a-zA-Z]/).test(arrayMe[g])) {
console.log(arrayMe[g])
fin.push(rot[g])
} else {
fin.push(arrayMe[g])
}
}
console.log(fin.join(''))
console.log(rot)
}
// soluction
function rot13(message) {
const alphabet = 'abcdefghijklmnopqrstuvwxyz'.split('');
let rot = [];
let fin = [];
const arrayMe = message.split('');
for (let i = 0; i < arrayMe.length; i++) {
let a = arrayMe[i];
if (/[a-zA-Z]/.test(a)) {
// Es una letra, hacer ROT13
let isUpperCase = a === a.toUpperCase();
let lowerA = a.toLowerCase();
let index = alphabet.indexOf(lowerA);
let newIndex = (index + 13) % 26;
let newChar = alphabet[newIndex];
if (isUpperCase) {
newChar = newChar.toUpperCase();
}
rot.push(newChar);
} else {
// No es una letra, mantener el carácter tal cual
rot.push(a);
}
}
fin = rot.join('');
return fin;
}
// Ejemplo de uso
console.log(rot13('Hello World!')); // Uryyb Jbeyq!
//The best soluction
function rot13(message) {
var a = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
var b = "nopqrstuvwxyzabcdefghijklmNOPQRSTUVWXYZABCDEFGHIJKLM"
return message.replace(/[a-z]/gi, c => b[a.indexOf(c)])
}
function rot13(message) {
var abc = 'abcdefghijklmnopqrstuvwxyzabcdefghijklmABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLM';
return message.replace(/[a-z]/gi, c => abc[abc.indexOf(c) + 13]);
}
const rot13 = str => str.replace(/[a-z]/gi, letter => String.fromCharCode(letter.charCodeAt(0) + (letter.toLowerCase() <= 'm' ? 13: -13)));
To embed this project on your website, copy the following code and paste it into your website's HTML: