The goals and limitations on the own implementation of replace are not entirely clear. It is possible as one of the options to consider the option in which you first use the split function, which splits a string using strToReplace as a separator. The result is an array in each cell of which the parts of the row without strToReplace will lie sequentially:
console.log("Я не люблю JavaScript и не люблю Василия".split("не люблю"));
and then combine this array into a string using the join function. Which will take all the elements of the array and make a string of them, inserting between the replacer elements.
It will turn out like this:
function replaceString(initString, strToReplace, replacer) { return initString.split(strToReplace).join(replacer); } console.log(replaceString("Я не люблю JavaScript и не люблю Василия", "не люблю", "люблю"));
Well, as for your implementation, you can only cosmetically:
make len constant because it never changes
const len = strToReplace.length;
and make the infinite loop condition normal true
while (true) {
lenin a loop shrink somewhere. Or change the condition of the cycle. - n3r0bi0m4n