-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day 7 objects.js
139 lines (108 loc) · 3.43 KB
/
Day 7 objects.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
// Activity 1: Object Creation and Access
// Task 1: Create an object representing a book and log it
let book = {
title: "To Kill a Mockingbird",
author: "Harper Lee",
year: 1960
};
console.log(book);
//Task 2: Access and log the title and author properties
console.log("Title:", book.title);
console.log("Author:", book.author);
// Activity 2: Object Methods
//Task 3: Add a method to return a string with the book's title and author
book.getDetails = function() {
return `${this.title} by ${this.author}`;
};
console.log(book.getDetails());
//Task 4: Add a method that updates the book's year property
book.updateYear = function(newYear) {
this.year = newYear;
};
book.updateYear(2021);
console.log(book);
// Activity 3: Nested Objects
// Task 5: Create a nested object representing a library and log it
let library = {
name: "City Library",
books: [
{ title: "1984", author: "George Orwell", year: 1949 },
{ title: "The Great Gatsby", author: "F. Scott Fitzgerald", year: 1925 },
book
]
};
console.log(library);
// Task 6: Access and log the name of the library and the titles of all the books
console.log("Library Name:", library.name);
library.books.forEach(b => console.log("Book Title:", b.title));
// Activity 4: The `this` Keyword
// Task 7: Add a method that uses `this` to return a string with the book's title and year
book.getTitleAndYear = function() {
return `${this.title} (${this.year})`;
};
console.log(book.getTitleAndYear());
//Activity 5: Object Iteration
//Task 8: Use a `for...in` loop to iterate over the properties of the book object
for (let key in book) {
if (book.hasOwnProperty(key)) {
console.log(`${key}: ${book[key]}`);
}
}
// Task 9: Use `Object.keys` and `Object.values` to log all keys and values
console.log("Keys:", Object.keys(book));
console.log("Values:", Object.values(book));
// Feature Request Scripts
// 1. Book Object Script
let bookScript = {
title: "To Kill a Mockingbird",
author: "Harper Lee",
year: 1960,
getDetails: function() {
return `${this.title} by ${this.author}`;
},
updateYear: function(newYear) {
this.year = newYear;
},
getTitleAndYear: function() {
return `${this.title} (${this.year})`;
}
};
console.log(bookScript);
console.log(bookScript.getDetails());
bookScript.updateYear(2021);
console.log(bookScript);
console.log(bookScript.getTitleAndYear());
// 2. Library Object Script
let libraryScript = {
name: "City Library",
books: [
{ title: "1984", author: "George Orwell", year: 1949 },
{ title: "The Great Gatsby", author: "F. Scott Fitzgerald", year: 1925 },
bookScript
]
};
console.log(libraryScript);
console.log("Library Name:", libraryScript.name);
libraryScript.books.forEach(b => console.log("Book Title:", b.title));
// 3. Object Iteration Script
let bookIteration = {
title: "To Kill a Mockingbird",
author: "Harper Lee",
year: 1960,
getDetails: function() {
return `${this.title} by ${this.author}`;
},
updateYear: function(newYear) {
this.year = newYear;
},
getTitleAndYear: function() {
return `${this.title} (${this.year})`;
}
};
for (let key in bookIteration) {
if (bookIteration.hasOwnProperty(key)) {
console.log(`${key}: ${bookIteration[key]}`);
}
}
console.log("Keys:", Object.keys(bookIteration));
console.log("Values:", Object.values(bookIteration));