The first thing i see, is a problem with .replace.
The replace method like you called it here replaces every matching Char. So if your programm runs, it changes the “E” in “SERR PBQR PNZC” to “R”, like it should. Then you get the string “FRRR PBQR PNZC”. So now your programm goes on and wants to change the third “R” to an “E”. But the .replace method replaces every “R” in your string with an “E”. So you get “SEEE PBQE PNZC”. So you constantly change your string, including the chars you already converted.
I’m not an expert, but i think you can’t change single chars in a string with the replace method. You need something else here.
Also for further use of the replace method: strings are immutable, str.replace(…) doesn’t change your str variable. You need to assign it to a new variable, like
var newStr = str.replace(…)
To solve this problem, i think you could define an empty string before your for loop. And then add the correct char to this string in every loop.
var newStr = “”
strings in JS are immutable objects and therefore you must assign the end result of replace to a new string, as the replace function does not alter the input string itself.
Replace also doesn’t work based on indices like what you tried to do. It finds the matches in the input string with your 1st argument, then replaces the first with the 2nd argument. Example:
const ex='I like food so so much';
const ex1=str.replace('like', 'hate');
const ex2=str.replace('so' , 'soo');
console.log(ex1) //prints 'I hate food so so much'
console.log(ex2) //prints 'I like food soo so much'
here’s some documentation on String.prototype.replace() on MDN.