-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday_7.js
222 lines (165 loc) · 3.9 KB
/
day_7.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
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
// Activity 1: Object Creation and Access
// Task 1
const book = {
title: "Six gates of hell",
author: "Jugadu Fauji",
year: 2023
};
console.log(book);
/* Output: { title: 'Six gates of hell',
author: 'Jugadu Fauji',
year: 2023 }
*/
// Task 2
console.log(`Title of the book is`, book.title);
console.log(`Author the book is`, book.author);
/* Output:
Title of the book is Six gates of hell
Author the book is Jugadu Fauji
*/
// Activity 2: Object Methods
// Task 3
// Can use the Object defineProperty method also
// Object.defineProperty(book, "titleAndAuthor", {
// get: function() {return this.title + " - " + this.author;}
// });
book.details = function() {
const detail = (this.title + " - " + this.author);
return detail;
}
console.log(book.details());
/* Output: Six gates of hell - Jugadu Fauji */
// Task 4
book.changeYear = function(newYear) {
this.year = newYear;
return this.year;
}
console.log("Year changed to", book.changeYear(2024));
/* Output: Year changed to 2024 */
// Activity 3: Nested Objects
// Task 5
const library = {
name: "Our Library",
books: [
{
title: "Six gates of hell",
author: "Jugadu Fauji",
year: 2023
},
{
title: "Kargil - Untold stories from the war",
author: "Rachna Bisht",
year: 2018
},
{
title: "The Brave: Param Vir Chakra Stories",
author: "Rachna Bisht",
year: 2019
},
]
};
console.log(library);
/* Output:
{
name: 'Our Library',
books: [
{ title: 'Six gates of hell', author: 'Jugadu Fauji' },
{
title: 'Kargil - Untold stories from the war',
author: 'Rachna Bisht'
},
{
title: 'The Brave: Param Vir Chakra Stories',
author: 'Rachna Bisht'
}
]
}
*/
// Task 6
console.log(library.name);
library.books.forEach((book) => {
console.log("Book Title -", book.title);
});
/* Output:
Our Library
Book Title - Six gates of hell
Book Title - Kargil - Untold stories from the war
Book Title - The Brave: Param Vir Chakra Stories
*/
// Activity 4: The this keyword
// Task 7
book.bookData = function() {
const data = (`${this.title} is published in ${this.year}`);
return data;
};
console.log(book.bookData());
/* Output: Six gates of hell is published in 2024 */
function getBookTitlesAndYears(library) {
const bookInfo = library.books.map(book => `${book.title} (${book.year})`);
return bookInfo;
};
console.log(getBookTitlesAndYears(library));
/* Output:
[
'Six gates of hell (2023)',
'Kargil - Untold stories from the war (2018)',
'The Brave: Param Vir Chakra Stories (2019)'
]
*/
// Activity 5: Object Iteration
// Task 8
for(let property in book) {
console.log(`${property} : ${book[property]}`);
}
/* Output:
title : Six gates of hell
author : Jugadu Fauji
year : 2024
details : function() {
const detail = (this.title + " - " + this.author);
return detail;
}
changeYear : function(newYear) {
this.year = newYear;
return this.year;
}
bookData : function() {
const data = (`${this.title} is published in ${this.year}`);
return data;
}
*/
// Task 9
console.log(Object.keys(book));
console.log(Object.values(book));
/* Output:
[ 'title', 'author', 'year', 'details', 'changeYear', 'bookData' ]
[
'Six gates of hell',
'Jugadu Fauji',
2024,
[Function (anonymous)],
[Function (anonymous)],
[Function (anonymous)]
]
*/
console.log(Object.keys(library));
console.log(Object.values(library));
/* Output:
[ 'name', 'books' ]
[
'Our Library',
[
{ title: 'Six gates of hell', author: 'Jugadu Fauji', year: 2023 },
{
title: 'Kargil - Untold stories from the war',
author: 'Rachna Bisht',
year: 2018
},
{
title: 'The Brave: Param Vir Chakra Stories',
author: 'Rachna Bisht',
year: 2019
}
]
]
*/