| 1 | var 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 | |
| 24 | console.log(urlify('Mr John Smith ', 13), 'Mr%20John%20Smith'); |