-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkata17URLDecode.js
More file actions
26 lines (24 loc) · 896 Bytes
/
kata17URLDecode.js
File metadata and controls
26 lines (24 loc) · 896 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/* Function will receive a url encoded string and return a
* Javascript object that represents the data.
* @param {string} text -- url encoded string
*/
const urlDecode = function(text) {
// separate text at '%20' and join to create whitespace
// separate text again at '&' and '='
let sep = text.split('%20').join(' ').split(/[&=]/);
console.log("sep=",sep);
// initiate empty decoded object
let decoded = {};
// loop to find key and value pairs
for (let i = 0; i < sep.length; i += 2) {
let key = sep[i];
let value = sep[i + 1];
console.log("loop ",i," key=",key," value=",value);
decoded[key] = value;
}
return decoded;
};
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").city);