She found a solution, but it is quite complicated and replaces spaces at the beginning and end of the line, but you need not to replace them, but to delete them. The solution should only be using a loop without String.prototype.replace. Please help with the solution.
function replaceSpaces(str) { str = str.split(''); let spaces = 0; for (let i = 0; i < str.length; i++) { if (str[i] === ' ') { spaces++; } } // i - ΡΠΊΠ°Π·Π°ΡΠ΅Π»Ρ Π½Π° ΡΠ΅ΠΊΡΡΠΈΠΉ ΡΠΈΠΌΠ²ΠΎΠ» ΠΈΡΡ
ΠΎΠ΄Π½ΠΎΠΉ ΡΡΡΠΎΠΊΠΈ // j - ΡΠΊΠ°Π·Π°ΡΠ΅Π»Ρ Π½Π° ΡΠ΅ΠΊΡΡΠΈΠΉ ΡΠΈΠΌΠ²ΠΎΠ» Π½ΠΎΠ²ΠΎΠΉ ΡΡΡΠΎΠΊΠΈ for (let i = str.length - 1, j = str.length + 2 * spaces; i >= 0; i--) { if (str[i] === ' ') { str[j - 1] = '0'; str[j - 2] = '2'; str[j - 3] = '%'; j -= 3; } else { str[j - 1] = str[i]; j -= 1; } } return str.join(''); } console.log(replaceSpaces("Lighthouse Labs")); console.log(replaceSpaces(" Lighthouse Labs ")); console.log(replaceSpaces("blue is greener than purple for sure")); The expected output should be:
Lighthouse%20Labs Lighthouse%20Labs blue%20is%20greener%20than%20purple%20for%20sure
encodeURI(" Lighthouse Labs ".trim())? - Rustam GimranovArray.from(' Light Labs '.trim(), letter => ' ' == letter ? '%20' : letter).join('')- Rustam Gimranov