Skip to content

Commit d0a8cde

Browse files
committed
added join solution from hatchways prep
1 parent c015b53 commit d0a8cde

File tree

2 files changed

+8
-40
lines changed

2 files changed

+8
-40
lines changed

hatchwaysHelp/aecio1-10-2020.js

Lines changed: 0 additions & 33 deletions
This file was deleted.

recursion/flattenDictionary.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
// The question is given an object / dictionary where the keys are strings,
2-
// and the values are either strings or another object / dictionary, can you
1+
// The question is given an object / dictionary where the keys are strings,
2+
// and the values are either strings or another object / dictionary, can you
33
// return a new object / dictionary with the keys flattened.
44

55
// For example, { a: "b", c: { d: "e", "f": {g: "e"} } }, would become { a: "b", "c.d": "e", c.f: "g" }.
6-
// Notice that this function will remove any of the nested objects / dictionaries,
6+
// Notice that this function will remove any of the nested objects / dictionaries,
77
// and will combine the keys in the nested objects / dictionaries to be joined by a period.
88

99
function flatten(dict) {
1010
// step through each property
1111
// if string, continue?
12-
// if object, append to the parent and
12+
// if object, append to the parent and
1313
const flattenedDictionary = {};
14-
15-
for (const key in dict){
14+
15+
for (const key in dict) {
1616
const value = dict[key];
1717
const parent = key;
1818
if (typeof value === 'object') {
@@ -38,4 +38,5 @@ function flattenHelper(parent, dict, flattenedDict) {
3838

3939

4040
console.log(flatten({ a: "b", c: { d: "e" } }));
41-
console.log(flatten({ a: "b", c: { d: "e", f: {e:"g"}} }))
41+
console.log(flatten({ a: "b", c: { d: "e", f: { e: "g" } } }))
42+
console.log(flatten({ a: "b", c: { d: "e", f: 'g' } }))

0 commit comments

Comments
 (0)