Amazon Online Assessment part II, Recursion
1899

My question was this:

given a url query string, test.foo.bar=123&let.me.see=456&what=isThis, parse the string into the following structure,

// {
//     "test": {
//         "foo": {
//             "bar": "123"
//         }
//     },
//     "let": {
//         "me": {
//             "see": "456"
//         }
//     },
//     "what": "isThis"
// }

this was the code I wrote,

function getKeyValueQueryParams(s) {
  let keyValues = s.split("&");

  let results = {};
  for (let i = 0; i < keyValues.length; i++) {
    let currentKeyValues = keyValues[i];
    results = { ...results, ...getKeyValuePairs(currentKeyValues) };
  }
  return results;

  function getKeyValuePairs(s) {
    let [keys, value] = s.split("=");
    let splitKey = keys.split(".");
    let outerKeys = {};
    helper(splitKey, outerKeys);
    return outerKeys;
    function helper(keys, currentNestedKey) {
      if (keys.length === 1) {
        let lastKey = keys[0];
        currentNestedKey[lastKey] = value;
        return;
      }
      let currentKey = keys[0];

      let newKey = {};
      currentNestedKey[currentKey] = newKey;
      helper(keys.slice(1), newKey);
    }
  }
}

I was informed that I'm moving on to on site.

Comments (4)