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 
  • Is the cycle required? just there is a special method for this - Stranger in the Q
  • @StrangerintheQ unfortunately cycle is required. Such a condition. - Anna
  • And so not encodeURI(" Lighthouse Labs ".trim()) ? - Rustam Gimranov
  • @RustamGimranov it works, but does not meet the condition) It is necessary only through the cycle - Anna
  • Another option is Array.from(' Light Labs '.trim(), letter => ' ' == letter ? '%20' : letter).join('') - Rustam Gimranov

2 answers 2

I guess this is the text that needs to be converted into a link. In JS, there is a default function that does this:

 var str = " salam aleykum"; console.log(encodeURIComponent(str.trim())) //Output: salam % 20 aleykum 

UPD:

 function replaceSpaces(text) { text = text.trim() var str = ""; for (var i = 0; i < text.length; i++) { if (text[i] == " ") { str += "%20"; continue; } else str += text[i]; } return str; } console.log(replaceSpaces(" red green blue")); 

  • cool, also works, but according to the conditions, you need to do it through a cycle) - Anna
  • Anna, updated .. - teamspam
  • Great, @teamspam!) Thank you so much! I would have your brains) - Anna

It seems so works. Is it necessary?

 let str = 'blue is greener than purple for sure'; let str1 = ' Space before'; let str2 = 'Space after '; function replaceSpaces(s) { let newStr = ''; for(let i = 0; i < s.length; i++) { if(s[i] === ' ') { if(i === 0) continue; if(i === s.length - 1) continue; newStr += '%20'; continue; } newStr += s[i]; } return newStr; } console.log(replaceSpaces(str)); console.log(replaceSpaces(str1)); console.log(replaceSpaces(str2)); 

  • the code works, but if there are spaces at the beginning and end of the line, they are also replaced with% 20, but you need to remove these spaces - Anna
  • @Anna aha !! )) Here, a little crutch, but it works :) Although, through a cycle, only such crutches. Using string methods or regulars would be very succinct and quick. - Alex Sazonov
  • Yes! Works) Of course using string methods or regular expressions would be faster and simpler, but the condition was met, so everything is OK) Thank you @AlexSazonov) - Anna
  • If there are two spaces at the beginning or at the end - it will not work ( - Stepan Kasyanenko