I can not understand why replace does not work, the task with freecodecamp (given a phrase in which words, letters from the first half of the alphabet need to be replaced with letters of the second half of the alphabet, respectively, ie A = N, B = O, etc.), all solutions associated with this task were through charCodeAt() , is it even possible to solve this problem through replace ?
function rot13(str) { const alfabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; const half1 = alfabet.split(''); // ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M"] const half2 = half1.splice(0,13); // ["N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"] const myStr = str.split(' '); for(let myShifrWords of myStr){ for(let i=0; i<myShifrWords.length;i++){ let e = myShifrWords[i]; function repl(e){ if(half1.indexOf(e)!==-1){ return half1[half2.indexOf(e)]; } else if(half2.indexOf(e)!==-1){ return half2[half1.indexOf(e)] } } return myShifrWords.replace(e, repl); } } } rot13("SERR CVMMN!")
A -> N, but vice versa? - entithatreturn myShifrWords.replace(e, repl);in the middle of the cycle - Pavel Mayorov