Caesars Cipher I just don't know why this simple code cannot solve the problem

Tell us what’s happening:

Your code so far


function rot13(str) { // LBH QVQ VG!
 let ab=['A','B','C','D','E','F','G','H','I','J','K','L','M'];let no=['N','O','P','Q','R','S','T','U','V','W','X','Y','Z'];
let ar = str.split('');
for(let i=0;i<ar.length;i++){if(ab.indexOf(ar[i])!==-1){let x=ab.indexOf(ar[i]);ar.splice(ar.indexOf(ar[i]),1,no[x]);}

else if(no.indexOf(ar[i])!==-1){let y=no.indexOf(ar[i]);
ar.splice(ar.indexOf(ar[i]),1,ab[y])
}

 }
  return ar.join('');
}

// Change the inputs below to test
rot13("SERR PBQR PNZC");

Your browser information:

User Agent is: Mozilla/5.0 (Linux; Android 6.0.1; SM-N910C) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.116 Mobile Safari/537.36.

Link to the challenge:

For starters, the first test case of rot13("SERR PBQR PNZC") returns 'FEEE PODR CAMC' instead of FREE CODE CAMP. Put some console.log statements in your code to see what values certain variables have and you should see what the problem is.

Finally I solved it with this code:

function rot13(str) { // LBH QVQ VG!
 let ab=['A','B','C','D','E','F','G','H','I','J','K','L','M'];let no=['N','O','P','Q','R','S','T','U','V','W','X','Y','Z'];
let ar = str.split('');
for(let i=0;i<ar.length;i++){if(ab.indexOf(ar[i])!==-1){let x=ab.indexOf(ar[i]);ar.splice(i,1,no[x]);}

else if(no.indexOf(ar[i])!==-1){let x=no.indexOf(ar[i]);
ar.splice(i,1,ab[x]);
}

 }
  return ar.join('');
}

Unhappyly it took me too much time. That’s why I don’t think there is some kind of ‘‘spoilers’’ right there. I’d rather call them ‘‘time savers’’ or just ‘‘helpers’’ because starters like me get stuck too much and that is very discouraging. Skipping challenges is heartbreaking. ‘‘spoilers’’ hold our hands when we lose faith.
Thanks again