JavaScript - Caesar cipher



One of the simplest and most widely known ciphers is a Caesar cipher, also known as a shift cipher. In a shift cipher the meanings of the letters are shifted by some set amount. A common modern use is the ROT13 cipher, where the values of the letters are shifted by 13 places. Thus 'A' ↔ 'N', 'B' ↔ 'O' and so on. Write a function that takes a ROT13 encoded string as input and returns a decoded string. All letters will be uppercase. Do not transform any non - a


//Solution Basic approach
console = { 
    log: print,
    warn: print,
    error: print
};

function rot13(str) {
    let decodeStr = ""
    for (let i = 0; i < str.length; i++) {
        let lettersCode = (str[i].charCodeAt());
        if (lettersCode >= 65 && lettersCode <= 77) {
            decodeStr += String.fromCharCode(lettersCode + 13);
        } else if (lettersCode >= 78 && lettersCode <= 90) {
            decodeStr += String.fromCharCode(lettersCode - 13);
        } else {
            decodeStr += str[i];
        }
    }
    return decodeStr;
}
console.log(rot13("SERR PBQR PNZC"));

Output:


FREE CODE CAMP

Solution not working or have any suggestions? Please send an email to [email protected]



Join Our Facebook Group


Share this example


Download Android App

download android app

Popular javascript Examples









codesadda.com

Codesadda.com is your home of programming solutions, tutorials, video tutorials and much more. Sign Up for our weekly newsletter to get update about new content.

Like us on Facebook | Connect with us on LinkedIn | Subscribe our Channel on Youtube