Skip to content

Instantly share code, notes, and snippets.

@Vampeyer
Created August 27, 2024 03:22
Show Gist options
  • Save Vampeyer/00896b8a58103e07a8b5b0553c3c8392 to your computer and use it in GitHub Desktop.
Save Vampeyer/00896b8a58103e07a8b5b0553c3c8392 to your computer and use it in GitHub Desktop.
JavaScript Mastery - Kata 5
// In this exercise, you will be given a normal string
// of words and will turn it into a percent-encoded string
// by replacing all whitespace with %20.
// Percent Encoding
// Take a look at the following URL, specifically the last
// part:
// This URL will perform a Google search for the term "lighthouse labs".
// Notice that when the string "lighthouse labs" is part of a URL, the
// space is replaced with %20.
// If you want to add a parameter to a URL, there are certain characters
// that must be encoded in order to make the URL valid. There are many
// characters that must be encoded, including: , !, ", and #.
// For this exercise, you will only be focusing on replacing the space
// with %20.
// Input
// const urlEncode = function(text) {
// // Put your solution here
// };
// console.log(urlEncode("Lighthouse Labs"));
// console.log(urlEncode(" Lighthouse Labs "));
// console.log(urlEncode("blue is greener than purple for sure"));
// Expected Output
// Lighthouse%20Labs
// Lighthouse%20Labs
// blue%20is%20greener%20than%20purple%20for%20sure
// Instruction
// Create a function named urlEncode that will receive a string of words,
// and return that string with all of the whitespace characters converted
// to %20. If there is whitespace on the outside of the string,
// like in the second example
// above, you should only replace the whitespace within the string.
let url = " this is a sample"
function urlEncodeSpace(url){
let encodedString = ''
for ( i in url){
// console.log(url[i])
if(url[i] === ' '){
encodedString += "%20"
}else {
encodedString += url[i]
}
if(encodedString[0] === " "){
encodedString.pop()
}
}
return encodedString;
}
console.log(urlEncodeSpace(url))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment