Skip to content

Rrg ctci #33

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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');