Created
August 11, 2020 20:23
-
-
Save cplpearce/10d205c53a7714eee351274155295b05 to your computer and use it in GitHub Desktop.
Kata 14 - JS Object From URL Encoded String
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const urlDecode = function(text) { | |
text = text.replace(/%20/g, ' '); | |
let strArray = text.split(/[(=)|(&)]+/); | |
let results = {}; | |
for (let i = 0; i < strArray.length; i += 2) | |
results[strArray[i]] = strArray[i + 1]; | |
return results | |
}; | |
console.log(urlDecode("duck=rubber")); | |
console.log(urlDecode("bootcamp=Lighthouse%20Labs")); | |
console.log(urlDecode("city=Vancouver&weather=lots%20of%20rain")); | |
console.log(urlDecode("city=Vancouver&weather=lots%20of%20rain").weather); | |
/* | |
{duck: "rubber"} | |
{bootcamp: "Lighthouse Labs"} | |
{city: "Vancouver", weather: "lots of rain"} | |
"lots of rain" | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment