Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions chapter01/1.3 - URLify/urlify.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,34 @@ var urlify = function(str, length) {
strArr[pointer+1] = '2';
strArr[pointer+2] = '0';
console.log(strArr, strArr.length);
}
}
pointer++;
}
// if character is a space, move remainder chars by two
// replace following three chars with '%20'
return strArr.join('');
};

console.log(urlify('Mr John Smith ', 13), 'Mr%20John%20Smith');
// takes 2 arguments a string and length of true strin
function URLify2(str, len) {
// setup i to be 0, use to iterate
// newStr will be use to concat character from str
let i = 0, newStr = '';

// while i is less than the len
while(i<len) {
// checks every character if str if its a space, if true
if(str[i] === ' ') {
// concat the newStr with '%20'
newStr += '%20';
} else {
// if condition is false or its not space concat characters from str to newStr
newStr += str[i];
}
// increment i
i++;
}
// return newStr
return newStr;
}
console.log(URLify2('Mr John Smith ', 13), 'Mr%20John%20Smith');