MCPcopy Create free account
hub / github.com/careercup/CtCI-6th-Edition-JavaScript / urlify

Function urlify

chapter01/1.3 - URLify/urlify-3.js:1–18  ·  view source on GitHub ↗
(str, len)

Source from the content-addressed store, hash-verified

1function urlify(str, len) {
2 let s = "";
3 let totalSpaces = str.length - len;
4 let frontSpaces = 0;
5 let flag = false;
6 for (let i = 0; i < str.length; i++) {
7 if (flag === false) {
8 if (str[i] === " ") frontSpaces++;
9 else flag = true;
10 }
11 if (flag === true && i < str.length - (totalSpaces - frontSpaces)) {
12 if (str[i] === " ") s += "%20";
13 else s += str[i];
14 }
15 }
16
17 return s;
18}
19
20console.log(urlify("Mr John Smith ", 13));

Callers 1

urlify-3.jsFile · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected