Caesars Cipher -Solved

Can we solve the same in another way?

Try it and find out! Get used to exploring ideas in code rather than being told what you can and cannot do.

1 Like
function rot13(str){
     wordA = 'A'.charCodeAt(0);
     wordN = 'N'.charCodeAt(0);
     wordZ = 'Z'.charCodeAt(0);
freshArr = [];
 
for (var i = 0;i<str.length;i++) {
    var word = str.charCodeAt(i);
    if(word >=wordN){
     freshArr.push(String.fromCharCode(word-13))
    }
    else{
        freshArr.push(String.fromCharCode(word+13));
    }
}
return freshArr.join("");
    }
    console.log(rot13('SERR PBQR'));
    ```

Iā€™m almost done with this but my O/P has a ā€œ-ā€ in between.

function rot13(str) 
{
  let deciphered =[];
  
  for (let i=0; i < str.length; i++)
  {
    let unicode = str[i].charCodeAt(0);
    
    if (unicode >= 65 && unicode <= 90)
    {
      unicode + 13 <= 90 ? unicode += 13: unicode = 65 + ((unicode + 13) % 59);  
    }
    
    deciphered.push(String.fromCharCode(unicode));
  }  


  return deciphered.join("").toUpperCase();
}