Skip to content

Instantly share code, notes, and snippets.

@lienista
Last active August 28, 2018 07:37
Show Gist options
  • Select an option

  • Save lienista/d5b3e8dbf5897e3283ed1aa9721042af to your computer and use it in GitHub Desktop.

Select an option

Save lienista/d5b3e8dbf5897e3283ed1aa9721042af to your computer and use it in GitHub Desktop.
Algorithms in Javascript: CTCI 1.3 URLify: Write a function to create an URL by replacing all spaces of an URL string with %20
const createUrl = (s1) => {
let s1Array = s1.split(' ');
let len = s1Array.length - 1;
if(s1Array[len] === '') {
s1Array.pop();
s1 = s1Array.join(' ');
return createUrl(s1);
}
return s1Array.join('%20');
}
let u = 'javascript algorithms ';
console.log(createUrl(u));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment