-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday7.js
203 lines (154 loc) · 4.57 KB
/
day7.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
// day 7 : Objects
// activity 1: object and creation and access
// task 1: create an object representing a book with properties like title, author and year and log the object to the console.
const book = {
title: "To kill Mockingbird",
author: "Harper Lee",
year: 1960,
}
// console.log(book);
// task 2: access and log title and author properties of the object.
// console.log(book.title);
// console.log(book.author);
// activity 2: object method
// task 3: add a method to the book object that return a string with the book's title and author, and log the result of calling this method.
const book2 = {
title: "To kill Mockingbird",
author: "Harper Lee",
year: 1960,
getBookinfo: function () {
return `${this.title} by ${this.author}`
}
}
// console.log(book2.getBookinfo());
// task 4: add a method to the book object that a parameter and updates the book's propery, then log the upload object.
const book3 = {
title: "To Kill a Mockingbird",
author: "Harper Lee",
year: 1960,
getBookInfo: function () {
return `${this.title} by ${this.author}`;
},
updateBookInfo: function (property, value) {
this[property] = value;
}
};
book3.updateBookInfo('title', 'Go Set a Watchman');
book3.updateBookInfo('year', 2015);
console.log(book3);
// activity 3: nested object
// task 5: create a nested object representing a library with properties like name and books's and log the library object to the console.
const library = {
name: "Central Library",
books: [
{
title: "The Ministry of Utmost Happiness",
author: "Arundhati Roy",
year: 2017
},
{
title: "Poonachi: Or The Story of a Black Goat",
author: "Perumal Murugan",
year: 2018
},
{
title: "The God of Small Things",
author: "Arundhati Roy",
year: 1997
},
{
title: "A Suitable Boy",
author: "Vikram Seth",
year: 1993
}
]
};
console.log(library);
// task 6: access and log the name of the library and the title of all the books in the library.
console.log("Library Name:", library.name);
library.books.forEach(book => {
console.log("Book Title:", book.title);
});
// activity 4: the this keyword
// task 7: add a method to the book object that uses the this keyword to return a string with the book's title and year and log the result of calling this method.
const poostkalayLibrary = {
name: "Poostkalay",
books: [
{
title: "The Ministry of Utmost Happiness",
author: "Arundhati Roy",
year: 2017,
getTitleAndYear: function () {
return `${this.title} (${this.year})`;
}
},
{
title: "Poonachi: Or The Story of a Black Goat",
author: "Perumal Murugan",
year: 2018,
getTitleAndYear: function () {
return `${this.title} (${this.year})`;
}
},
{
title: "The White Tiger",
author: "Aravind Adiga",
year: 2008,
getTitleAndYear: function () {
return `${this.title} (${this.year})`;
}
},
{
title: "The Inheritance of Loss",
author: "Kiran Desai",
year: 2006,
getTitleAndYear: function () {
return `${this.title} (${this.year})`;
}
},
{
title: "A Suitable Boy",
author: "Vikram Seth",
year: 1993,
getTitleAndYear: function () {
return `${this.title} (${this.year})`;
}
}
]
};
console.log(`Library Name: ${poostkalayLibrary.name}`);
poostkalayLibrary.books.forEach(book => {
console.log(book.getTitleAndYear());
});
// activity 5: object iteration
// task 8: use a for...in loop to over the properties of the book object and log each property and it's value.
const book4 = {
title: "The White Tiger",
author: "Aravind Adiga",
year: 2008
};
for (let property in book) {
if (book4.hasOwnProperty(property)) {
console.log(`${property}: ${book4[property]}`);
}
}
// task 9: use object.key and object.value method to log all the keys and values of the book object.
const book5 = {
title: "The White Tiger",
author: "Aravind Adiga",
year: 2008
};
const keys = Object.keys(book5);
console.log("Keys:");
keys.forEach(key => {
console.log(key);
});
const values = Object.values(book5);
console.log("\nValues:");
values.forEach(value => {
console.log(value);
});
console.log("\nKeys and Values:");
keys.forEach((key, index) => {
console.log(`${key}: ${values[index]}`);
});