Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
170 lines (120 loc) · 4.55 KB
/
Copy pathscript.js
File metadata and controls
170 lines (120 loc) · 4.55 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
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
Game of Thrones : Houses And People's Name
Try to first understand how Array in Objects & Objects in Array are arranged in this example.
Only when you don't get it by yourself, comeback again and read the hint
*/
// Write code for the following problems:
console.log(got);
// You have access to a variable named `got`. Which comes from `data.js` file
/*
Hint: (Structure of got variable)
- got variable is an Object which has one key: `houses`
- `houses` is an array of multiple house
- Each house is an Object which has two keys: "name" and "people"
- people is an array of all persons living in that particular house
- And, each person is an object with "name" and "description" properties
*/
// 1. Log the value of key "houses"
console.log(got.houses);
// 2. Log the length of the "houses" key
console.log(got.houses.length);
// 3. Log the name of all houses in got one by one
for(let houses of got.houses) {
console.log(houses.name);
}
// 4. Log the name of all the people in house Starks (index 0) one by one
for(let member of got.houses[0].people) {
console.log(member.name);
}
// 5. Log the name of all the people in house Lannisters (index 1) one by one
for(let member of got.houses[1].people) {
console.log(member.name);
}
// 6. Log the description of all the people in house Lannisters (index 1) one by one
for(let member of got.houses[1].people) {
console.log(member.description);
}
// 7. Log the name and description of all the people in house Lannisters (index 1) one by one like `I am [name] and my bio is [description]`
for(let member of got.houses[1].people) {
console.log(`I am ${member.name} and my bio is ${member.description}`);
}
// 8. Log the name and description of all the people in house Starks (index 0) one by one like `I am [name] and my bio is [description]`
for(let member of got.houses[0].people) {
console.log(`I am ${member.name} and my bio is ${member.description}`);
}
// 9. Log the name and description of the people in house Starks whose name is `Robb Stark`
for(let member of got.houses[0].people) {
if(member.name == "Robb Stark") {
console.log(`I am ${member.name} and my bio is ${member.description}`);
}
}
// 10. Log the name and description of the people in house Lannisters whose name is `Tywin Lannister`
for(let member of got.houses[1].people) {
if(member.name == "Tywin Lannister") {
console.log(`I am ${member.name} and my bio is ${member.description}`);
}
}
// 11. Push the name of the houses in an array named `houseNames` and Log the array
let houseNames = [];
for (let house of got.houses) {
houseNames.push(house.name);
}
console.log(houseNames);
// 12. Log the size of people in Starks house
console.log(got.houses[0].people.length);
// 13. Log the size of people in Lannisters house
console.log(got.houses[1].people.length);
// 14. Add the name and size of people in an object like {Starks: 4, Lannisters: 6} and log the object
// console.log(`${got.houses[0].name}: ${got.houses[0].people.length}, ${got.houses[1].name}: ${got.houses[1].people.length}`);
// or
let houseInfo = [];
for(let house of got.houses) {
houseInfo[house.name] = house.people.length;
}
console.log(houseInfo);
// 15. Log the name of all the people of all the houses in got one by one
for (let house of got.houses) {
for(let people of house.people){
console.log(people.name);
}
}
// 16. Push all names into a new array named `allPeople` and log the value array.
let allPeople = [];
// for(let member of got.houses[0].people) {
// allPeople.push(member.name)
// }
// for(let member of got.houses[1].people) {
// allPeople.push(member.name);
// }
// console.log(allPeople);
// or
for (let house of got.houses) {
for(let people of house.people){
allPeople.push(allPeople.name);
}
}
// 17. Log the value and length of `allPeople` array created above
console.log(allPeople.length);
// 18. Push all descriptions into an array named `allDescription`
let allDescription = [];
// for(let member of got.houses[0].people) {
// allDescription.push(member.description)
// }
// for(let member of got.houses[1].people) {
// allDescription.push(member.description);
// }
// console.log(allDescription);
// or
for (let house of got.houses) {
for(let people of house.people){
allDescription.push(people.description);
}
}
// 19. Log the value and length `allDescription`
console.log(allDescription.length);
// 20. Only the the description whose length is more than 30. (use .length property on string to get the length)
for (let elment of allDescription) {
if (elment.length > 30) {
console.log(elment);
}
}