Skip to content

Instantly share code, notes, and snippets.

@kevinahn7
Created December 13, 2018 19:10
Show Gist options
  • Save kevinahn7/3fa8eed7f0db0d7d91d86a1307fe2d06 to your computer and use it in GitHub Desktop.
Save kevinahn7/3fa8eed7f0db0d7d91d86a1307fe2d06 to your computer and use it in GitHub Desktop.
Question from CtCI
let test = "Mr John Smith ";
function urlify(string) {
let stringArray = string.split(" ");
let filteredArray = stringArray.filter(x => x != "")
let newFilteredArray = filteredArray.join(" ").split("")
for (let x = 0; x < newFilteredArray.length; x++) {
if (newFilteredArray[x] === " ") {
newFilteredArray[x] = "%20"
}
}
return newFilteredArray.join("")
}
console.log(urlify(test))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment