-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
66 lines (60 loc) · 1.28 KB
/
index.js
File metadata and controls
66 lines (60 loc) · 1.28 KB
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
let users = {
Alan: {
age: 27,
online: false
},
Vlad: {
age: 21,
online: true
},
Jeff: {
age: 32,
online: true
},
Sarah: {
age: 48,
online: false,
// this is a hidden object. We will find with with foundHidden function
hidden: {
mister: "X"
}
},
Ryan: {
age: 19,
online: true,
hidden: {
mister: "X"
}
}
}
let countOnline = (obj) => {
let num = 0
// iterate object with for...in loop
for (let user in obj) {
obj[user]['online'] ? num++ : null
}
return num
}
let off = 0
let countOff = (obj) => {
for(person in obj) {
obj[person]['online'] != true ? off++ : null
}
return off
}
const foundHidden = (obj) => {
let result = ''
const keysArr = Object.keys(obj)
// mapping over the array of object's keys
keysArr.map((item, index) => {
// we need check here to not break the loop when hidden is undefined
obj[item]["hidden"] !== undefined ?
// when it is defined we return value from the array of keys with current index
(obj[item]["hidden"]['mister'] == 'X' ? result += " found in " + Object.getOwnPropertyNames(obj)[index] : null) :
null
})
return result
}
console.log(foundHidden(users))
console.log(countOnline(users))
console.log(countOff(users))