Ask a Question
Ask Question Login
Corporate Training
  1. Community
  2. Java
  3. Question
Java

Explain rot13 javascript.

Asked by Ria Papuii Colney Oct 13, 2022 184 views 1 answer
Share

About this question

I wrote a ROT13 cipher script rotating uppercase letters only. Can this function be improved?

ROT13 Wikipedia article

function rot13(str) {

var codeA = "A".charCodeAt(0);

var codeN = "N".charCodeAt(0);

var codeZ = "Z".charCodeAt(0);

var newArr = [];


for(var i =0; i

    var code = str.charCodeAt(i);

    if(code>=codeA && code<=codeZ){

        if(code>=codeN)

            newArr.push(String.fromCharCode(code-13));

        else

            newArr.push(String.fromCharCode(code+13));

    }else{

        newArr.push(str[i]);}

    }

     return newArr.join("");

}


Your answer

1 Answer

More Java discussions