Skip to content

Instantly share code, notes, and snippets.

@berdfandrade
Created January 16, 2024 18:54
Show Gist options
  • Save berdfandrade/4fbc54bf8e53227faeb7bb986a51814d to your computer and use it in GitHub Desktop.
Save berdfandrade/4fbc54bf8e53227faeb7bb986a51814d to your computer and use it in GitHub Desktop.
function transformString(str) {
let transformed = "";
for (let i = 0; i < str.length; i++) {
let letter = str[i];
if (/[a-zA-Z]/.test(letter)) {
let ascii = letter.charCodeAt(0);
ascii += 13;
if ((ascii > 122) || (ascii > 90)) {
ascii -= 26;
}
transformed += String.fromCharCode(ascii);
} else {
transformed += letter;
}
}
return transformed;
}
function rot13(str) {
let arr = [str];
console.log(arr);
let newArr = [];
let novo;
for(let i = 0; i < arr.length; i++){
for(let j = 0; j < arr[i].length; j++){
newArr.push(transformString(arr[i][j]))
}
}
novo = newArr.join("")
console.log(novo)
return novo
}
rot13("SERR CVMMN!");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment