Hi all, I have just finished “Caesars Cipher” on Javascript Algorithm curriculum. I hope you can give me feedback on my work. It took me 2 hours something improving on my program.
function rot13(str) {
const MIN = 65,
MAX = 90,
MID = 77,
ROT_13 = 13;
const newName = str.split(" ");
let charArray = [];
function numToAscii(val) { //function to return an ASCII value of a decimal number
return String.fromCharCode(val);
}
for (let i in newName) {
charArray[i] = newName[i].split("").map( //do the split, map and join
function (charValue) {
let numValue = charValue.charCodeAt(0); //get character decimal value
if (numValue < MIN || numValue > MAX) {
return numToAscii(numValue); //do nothing, just return the decimal value if char is not a letter
} else if (numValue > MID) {
return numToAscii((MIN - 1) + (ROT_13 - (MAX - numValue))); //in case decimal value is more than MID return calculated result
} else {
return numToAscii(numValue + 13); //add 13 to decimal value
}
}
).join("");
}
return charArray.join(" ");
}
rot13("SERR PBQR PNZC");