-
Notifications
You must be signed in to change notification settings - Fork 0
/
task5Rec.js
97 lines (92 loc) · 3.16 KB
/
task5Rec.js
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// Task 5 - Two solutions is provided one "mercifull" and one "strict"
const hierarchy = {
salesDepartment: {
leader: {
name: 'John Doe', wage: 700000,
assistant: {
name: 'Jane Doe', wage: 200000
},
},
sellers: [{
name: 'Maximillian Kuvi',
wage: 300000
},
{
name: 'Olav Ityr', wage: 330000
},
{
name: 'Scot Sheila', wage: 380000
},]
}, techDepartment: {
head: {
name: 'Mats Eilert', wage: 800000,
assistant: {
name: 'Karina Boury', wage: 360000,
HRContact: {
name: 'Johan Rufus', wage: 460000
}
},
},
developers: [{
name: 'Hagop Darren',
wage: 340000
},
{
name: 'Fredrik Sverre', wage: 430000
},
{
name: 'Crystal Manolo', wage: 680000
}]
}
}
const findEmployeMercifull = (name = "") => {
// Since this solution is "mercifull" i sat the default value of the argument to an empty string
const findings = []
const employeSearch = (searchObject) => {
// For loop through the hierarcyObject.
for (prop in searchObject) {
// If the property is name get the name
if (prop === 'name') {
// This is a "mercifull" function so we use "includes" to allow som space mistyping
// And weusing toLowerCase to allow "random" capitalisation of the name in the search
if (searchObject[prop].toLowerCase().includes(name.toLowerCase())) {
const { name, wage } = searchObject
empolye = { name, wage }
findings.push(empolye)
}
// If the property is an object send it through the function again.
} else if (typeof searchObject[prop] === 'object') {
employeSearch(searchObject[prop])
}
}
}
employeSearch(hierarchy)
// console.log(findings)
return findings
}
const findEmployeStrict = (name) => {
let employeFindings
const employeSearch = (searchObject) => {
// For loop through the object.
for (prop in searchObject) {
// If the property is name get the name.
if (prop === 'name') {
// Since this function is more "strict" we construct this function to not allow misspelling
// in the input or wrong capitalization of the name
if (searchObject[prop] === name) {
const { name, wage } = searchObject
employeFindings = { name, wage }
return employeFindings
}
// If the property is an object send it through the function again.
} else if (typeof searchObject[prop] === 'object') {
employeSearch(searchObject[prop])
}
}
}
employeSearch(hierarchy)
// console.log(employeFindings)
return employeFindings
}
findEmployeMercifull("Doe")
findEmployeStrict("John Doe")