FCC Challenge: JS: Caesars Cipher

Solution
function rot13(str) {
  let alph ="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  let conv ="";
  let getIndex = num => num+13 >= alph.length ? num+13-alph.length : num+13;
  for(let i=0; i<str.length; i++){
    if(alph.indexOf(str[i])!=-1){
      conv=conv.concat(alph[getIndex(alph.indexOf(str[i]))]);
    }else{
      conv=conv.concat(str[i])
    }
  }
  console.log(conv);
  return conv;
}

rot13("SERR PBQR PNZC");

alph.length is 26, so instead of doing num+13-alph.length you can just type num-13

1 Like