Skip to content

Commit 1ff0aa2

Browse files
committed
day 7
1 parent 5998c45 commit 1ff0aa2

File tree

1 file changed

+139
-0
lines changed

1 file changed

+139
-0
lines changed

Day 7 objects.js

+139
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
// Activity 1: Object Creation and Access
2+
3+
// Task 1: Create an object representing a book and log it
4+
5+
let book = {
6+
title: "To Kill a Mockingbird",
7+
author: "Harper Lee",
8+
year: 1960
9+
};
10+
console.log(book);
11+
12+
13+
//Task 2: Access and log the title and author properties
14+
console.log("Title:", book.title);
15+
console.log("Author:", book.author);
16+
17+
18+
// Activity 2: Object Methods
19+
20+
//Task 3: Add a method to return a string with the book's title and author
21+
book.getDetails = function() {
22+
return `${this.title} by ${this.author}`;
23+
};
24+
console.log(book.getDetails());
25+
26+
//Task 4: Add a method that updates the book's year property
27+
book.updateYear = function(newYear) {
28+
this.year = newYear;
29+
};
30+
book.updateYear(2021);
31+
console.log(book);
32+
33+
34+
// Activity 3: Nested Objects
35+
36+
// Task 5: Create a nested object representing a library and log it
37+
let library = {
38+
name: "City Library",
39+
books: [
40+
{ title: "1984", author: "George Orwell", year: 1949 },
41+
{ title: "The Great Gatsby", author: "F. Scott Fitzgerald", year: 1925 },
42+
book
43+
]
44+
};
45+
console.log(library);
46+
47+
// Task 6: Access and log the name of the library and the titles of all the books
48+
console.log("Library Name:", library.name);
49+
library.books.forEach(b => console.log("Book Title:", b.title));
50+
51+
52+
// Activity 4: The `this` Keyword
53+
54+
// Task 7: Add a method that uses `this` to return a string with the book's title and year
55+
56+
book.getTitleAndYear = function() {
57+
return `${this.title} (${this.year})`;
58+
};
59+
console.log(book.getTitleAndYear());
60+
61+
62+
//Activity 5: Object Iteration
63+
64+
//Task 8: Use a `for...in` loop to iterate over the properties of the book object
65+
for (let key in book) {
66+
if (book.hasOwnProperty(key)) {
67+
console.log(`${key}: ${book[key]}`);
68+
}
69+
}
70+
71+
// Task 9: Use `Object.keys` and `Object.values` to log all keys and values
72+
console.log("Keys:", Object.keys(book));
73+
console.log("Values:", Object.values(book));
74+
75+
// Feature Request Scripts
76+
77+
// 1. Book Object Script
78+
79+
let bookScript = {
80+
title: "To Kill a Mockingbird",
81+
author: "Harper Lee",
82+
year: 1960,
83+
getDetails: function() {
84+
return `${this.title} by ${this.author}`;
85+
},
86+
updateYear: function(newYear) {
87+
this.year = newYear;
88+
},
89+
getTitleAndYear: function() {
90+
return `${this.title} (${this.year})`;
91+
}
92+
};
93+
console.log(bookScript);
94+
console.log(bookScript.getDetails());
95+
bookScript.updateYear(2021);
96+
console.log(bookScript);
97+
console.log(bookScript.getTitleAndYear());
98+
99+
100+
// 2. Library Object Script
101+
102+
let libraryScript = {
103+
name: "City Library",
104+
books: [
105+
{ title: "1984", author: "George Orwell", year: 1949 },
106+
{ title: "The Great Gatsby", author: "F. Scott Fitzgerald", year: 1925 },
107+
bookScript
108+
]
109+
};
110+
console.log(libraryScript);
111+
console.log("Library Name:", libraryScript.name);
112+
libraryScript.books.forEach(b => console.log("Book Title:", b.title));
113+
114+
115+
// 3. Object Iteration Script
116+
117+
let bookIteration = {
118+
title: "To Kill a Mockingbird",
119+
author: "Harper Lee",
120+
year: 1960,
121+
getDetails: function() {
122+
return `${this.title} by ${this.author}`;
123+
},
124+
updateYear: function(newYear) {
125+
this.year = newYear;
126+
},
127+
getTitleAndYear: function() {
128+
return `${this.title} (${this.year})`;
129+
}
130+
};
131+
132+
for (let key in bookIteration) {
133+
if (bookIteration.hasOwnProperty(key)) {
134+
console.log(`${key}: ${bookIteration[key]}`);
135+
}
136+
}
137+
138+
console.log("Keys:", Object.keys(bookIteration));
139+
console.log("Values:", Object.values(bookIteration));

0 commit comments

Comments
 (0)