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

Function urlify

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

Source from the content-addressed store, hash-verified

1var urlify = function(str, length) {
2 // have a pointer to check from start to end
3 var strArr = str.split('');
4 var pointer = 0;
5 while (pointer < str.length) {
6 if (strArr[pointer] === ' ') {
7 // *** needs more work here, a little wierd
8 // not handling trailing spaces properly
9 for (var i = str.length - 1; i > pointer + 3; i--) {
10 strArr[i] = str[i - 2];
11 }
12 strArr[pointer] = '%';
13 strArr[pointer+1] = '2';
14 strArr[pointer+2] = '0';
15 console.log(strArr, strArr.length);
16 }
17 pointer++;
18 }
19 // if character is a space, move remainder chars by two
20 // replace following three chars with '%20'
21 return strArr.join('');
22};
23
24console.log(urlify('Mr John Smith ', 13), 'Mr%20John%20Smith');

Callers 1

urlify.jsFile · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected