function replaceString(initString, strToReplace, replacer) { let result = initString; let i = 0; let len = strToReplace.length; while (len) { i = result.indexOf(strToReplace, i); if (i == -1) { break; } result = result.substring(0, i) + replacer + result.substring(i + len); i += len; } return result; } let newStr = replaceString("Я не люблю JavaScript", "не люблю", "люблю"); console.log(newStr) 

Tell me how to improve this feature.

Closed due to the fact that the essence of the question is not clear to the participants of Kromster , Suvitruf , default locale , cheops , ilyaplot Nov 2 '17 at 7:20 .

Try to write more detailed questions. To get an answer, explain what exactly you see the problem, how to reproduce it, what you want to get as a result, etc. Give an example that clearly demonstrates the problem. If the question can be reformulated according to the rules set out in the certificate , edit it .

  • 2
    and what does this feature do? what specifically would like to improve - Grundy
  • This function does the same as the String.replace method ... Here I have while (len) an infinite loop ... I don’t know how to change this - Anna
  • @ Anna len in a loop shrink somewhere. Or change the condition of the cycle. - n3r0bi0m4n
  • @ n3r0bi0m4n So everything works and so, the question is how to do better. - tilin
  • @tilin was asked a little higher what exactly I would like to do better, to which an answer about an infinite loop was received. Well, or I do not understand anything in the evening. - n3r0bi0m4n

3 answers 3

Why not use the standard replace function?

 function replaceString(initString, strToReplace, replacer) { return initString.replace(strToReplace, replacer); } let newStr = replaceString("Я не люблю JavaScript", "не люблю", "люблю"); console.log(newStr) 

Or if you need to replace all the "do not like":

 function replaceString(initString, strToReplace, replacer) { return initString.replace(new RegExp(strToReplace, 'g'), replacer); } let newStr = replaceString("Я не люблю JavaScript", "не люблю", "люблю"); console.log(newStr) 

  • in the latter case, you need to be careful - strToReplace can be a regular expression in which the specials will be interpreted. characters - mymedia

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) { 
  • I need exactly one function to create everything - Anna

The answer is already one, I will offer my version based on the idea of ​​the author:

 function replaceString(initString, strToReplace, replacer) { let result = initString; let i = 0; while ((i = result.indexOf(strToReplace, i)) != -1) { result = result.substring(0, i) + replacer + result.substring(i + strToReplace.length, result.length); } return result; } console.log(replaceString('Я не люблю JavaScript', 'не люблю', 'люблю')); console.log(replaceString('Тут нет таких слов', 'привет', 'пока')); console.log(replaceString('Я люблю яблоки и яблоки', 'яблоки', 'девочек'));